Core Academy: Core Certification (Part IV) - Scripting and Core API...Spawn Objects

I have students that are trying to work on "Core Certification" in Core Academy and we have run into an issue that we can't seem to fix (more than 4 weeks working on this...) . We are using the provided code to spawn and equip a hat but we just get an error (listed below). Provided below will be the link...the error code...the ShopServer code...and the UIShop (in case it is a part of the problem. As an FYI, I have used both "Hat" and "WizardHat" in the custom properties and neither of them work.

Here is the link for the exact page in Core Academy

Error running Lua task: [6C2795901343C52B] ShopServer:4: stack index 1, expected string, received nil: (bad argument into 'CoreObject(string, table)')

Here is the code from the ShopServer:
local HAT = script:GetCustomProperty("WizardHat")

function SpawnAndEquip(player, ref)
local equipment = World.SpawnAsset(ref)
equipment:Equip(player)
end

function OnBuyHat(player)
if player:GetResource("Coins") < 3 then
print("Error, not enough coins")
return
end
player:RemoveResource("Coins", 3)
SpawnAndEquip(player, HAT_REF)
Events.BroadcastToPlayer(player, "ToggleShop", false)
end

Events.ConnectForPlayer("BuyHat", OnBuyHat)

Here is the UIShop code:
local BUYHAT_BUTTON = script:GetCustomProperty("BuyHatButton"):WaitForObject()
local UI_CONTAINER = script:GetCustomProperty("UI Container"):WaitForObject()

-- Hide the container when starting the game
UI_CONTAINER.visibility = Visibility.FORCE_OFF

local HAT_PRICE = 3

local PLAYER = Game.GetLocalPlayer()

function OnBuyHat(button)
if PLAYER:GetResource("Coins") < HAT_PRICE then
print("Not enough coins")
return
end
Events.BroadcastToServer("BuyHat")

print("Just bought a hat")
-- Here we need to send the information to the server
-- Only the server can remove resources and spawn the equipment

end
BUYHAT_BUTTON.clickedEvent:Connect(OnBuyHat)

-- This function show and hide the UI
-- Triggered when another script send the "ToggleShop" event
function ToggleUI(show)
if show then
UI_CONTAINER.visibility = Visibility.INHERIT
else
UI_CONTAINER.visibility = Visibility.FORCE_OFF
end

-- Here, we are using show as this value equals true or false
UI.SetCursorVisible(show)
UI.SetCanCursorInteractWithUI(show)

end
Events.Connect("ToggleShop", ToggleUI)

The error is due to the HAT variable highlighted below:

Just rename one of them so that they both match up and that should fix the error.

local HAT_REF = script:GetCustomProperty("WizardHat")

function SpawnAndEquip(player, ref)
    local equipment = World.SpawnAsset(ref)
    equipment:Equip(player)
end

function OnBuyHat(player)
    if player:GetResource("Coins") < 3 then
        print("Error, not enough coins")
        return
    end
    player:RemoveResource("Coins", 3)
    SpawnAndEquip(player, HAT_REF)
    Events.BroadcastToPlayer(player, "ToggleShop", false)
end

Events.ConnectForPlayer("BuyHat", OnBuyHat)

--the things I will do for my students...-- Thanks for that...but...now it is telling me that (line 5) "equipment:Equip(player)" isn't working.

I am starting again on learning to code but that doesn't mean I am learning fast enough to come up with answers that are likely sitting there slapping me in the face and I don't even know it. Is it wanting me to reference the direct name of the character? in this case I am using the Humanoid 2 Rig...but if I reference a character directly wouldn't this make the use of any other character unusable...?

local HAT_REF = script:GetCustomProperty("WizardHat")

function SpawnAndEquip(player, ref)
local equipment = World.SpawnAsset(ref)
equipment:Equip(player)
end

function OnBuyHat(player)
if player:GetResource("Coins") < 3 then
print("Error, not enough coins")
return
end
player:RemoveResource("Coins", 3)
SpawnAndEquip(player, HAT_REF)
Events.BroadcastToPlayer(player, "ToggleShop", false)
end

Events.ConnectForPlayer("BuyHat", OnBuyHat)

HI, richardkaer

did you finally found the ussie in your script ?

i have the exactly same issue here

Error running Lua task: [75C1510D8C9E7B2B] ShopServer:5: attempt to call a nil value (method 'Equip')

My script for Shop Server:

local HAT_REF = script:GetCustomProperty("Hat")

function SpawnAndEquip(player, ref)
local Equipment = World.SpawnAsset(ref)
Equipment:Equip(player)
end

function OnBuyHat(player)
if player:GetResource("Coins") < 3 then
print("Error, not enough coins")
return
end
player:RemoveResource("Coins", 3)
SpawnAndEquip(player, HAT_REF)
Events.BroadcastToPlayer(player, "ToggleShop", false)
end

Events.ConnectForPlayer("BuyHat", OnBuyHat)

and UIShop:

-- Custom
local BUY_HAT_BUTTON = script:GetCustomProperty("BuyHatButton"):WaitForObject()
local UI_CONTAINER = script:GetCustomProperty("UI Container"):WaitForObject()

-- Hide the container when starting the game
UI_CONTAINER.visibility = Visibility.FORCE_OFF

local HAT_PRICE = 3

local PLAYER = Game.GetLocalPlayer()

function OnBuyHat(button)
if PLAYER:GetResource("Coins") < HAT_PRICE then
print("Not enough coins")
return
end
Events.BroadcastToServer("BuyHat")
end
-- Here we need to send the information to the server
-- Only the server can remove resources and spawn the equipment
UIShop
BUY_HAT_BUTTON.clickedEvent:Connect(OnBuyHat)

-- This function show and hide the UI
-- Triggered when another script send the "ToggleShop" event
function ToggleUI(show)
if show then
UI_CONTAINER.visibility = Visibility.INHERIT
else
UI_CONTAINER.visibility = Visibility.FORCE_OFF
end

-- Here, we are using show as this value equals true or false
UI.SetCursorVisible(show)
UI.SetCanCursorInteractWithUI(show)

end
Events.Connect("ToggleShop", ToggleUI)

I haven't...the new error dealing with the player is: equipment:Equip(player). I was thinking that it is having issues with needing to have a local identifier (may be the wrong terminology) for the player but we haven't found anything yet.

Mine is working....on the spawn object page go slowly and watch where you are placing the code. There was a vague reference that I think I missed previously. It is very vague (or I saw it that way and I am perfect so...).

Here is the "game" so you can open it up and compare the code. I will need to do the same to see what the students have been doing wrong. Core Certification Part IV by richardkaer - Core Games