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)