In this lesson, we are going to spawn the hat when the player buys it and equip it.
Spawning an equipment
To spawn any objects in your game, you need an asset reference. This asset reference is a template that you have made.
Create a template
For this example, a new template has been made using a CC called “witch hat model” by the creator, mangoboy. This model was dropped in a Client Context of an empty Equipment object and after changing the color, we have a cool hat that looks like our UI.
Hierarchy of our template with the position and scale of the hat
The Wizard Hat
Once you are happy with your new hat, you can create a new “Template From This” and drag that template as a Custom Property of your ShopServer.
The template inside the Project Content (networked as it will be spawned from a server-side script)
The Asset Reference of the Hat as a Custom Property of the ShopServer script
SpawnAsset and Equip
To spawn an object, you can use the function World.SpawnAsset. This can be done on the server if the object is networked, or in the Client Context even if the object is not networked. World.SpawnAsset returns a reference to the spawned object so we can store that in a variable called equipment and use Equip to attach it to the player.
functionSpawnAndEquip(player, ref)local equipment = World.SpawnAsset(ref)
equipment:Equip(player)
end
Code language:Lua(lua)
This function is going to be called after the Coins are removed from the player. The HAT_REF is the Custom Property that contains the reference to the template we want to spawn. Here is the full script:
local HAT_REF = script:GetCustomProperty("Hat")
functionSpawnAndEquip(player, ref)local equipment = World.SpawnAsset(ref)
equipment:Equip(player)
endfunctionOnBuyHat(player)if player:GetResource("Coins") < 3thenprint("Error, not enough coins")
returnend
player:RemoveResource("Coins", 3)
SpawnAndEquip(player, HAT_REF)
end
Events.ConnectForPlayer("BuyHat", OnBuyHat)
Code language:Lua(lua)
Now the hat is spawned and equipped to the player
Bonus step – Hiding Shop after purchase
As you might have noticed in the previous gif, the UI is hidden when the player buys the hat. To do that, we can use our ToggleShop event that is connected to our UIShop script. This is how you can broadcast an event to a specific player. Here we are sending an event “ToggleShop” to the player with the parameter false to hide the UI.
While making your game, you must give a goal to the player. Why do they want this hat? In this example, we are going to give twice the amount of Coins if the player has the WizardHat.
Open the PickupCoin script in Project Content > My Scripts
Paste the following code
Line 4: this function is returning true if, in all the items in the Equipment of the player, one item is called “WizardHat”. Line 17: if this condition is true, give 2 coins, else, give 1 coin.
local TRIGGER = script:GetCustomProperty("Trigger"):WaitForObject()
local COIN = script.parent
functionPlayerHasHat(player)local equipment = player:GetEquipment()
for _,item inipairs(equipment) doif item.name == "WizardHat"then-- Replace with the name of your template if you changed itreturntrueendendreturnfalseendfunctionOnBeginOverlap(trigger, player)ifnot player:IsA("Player") thenreturnendif PlayerHasHat(player) then player:AddResource("Coins", 2)
else player:AddResource("Coins", 1)
end COIN:Destroy()
endTRIGGER.beginOverlapEvent:Connect(OnBeginOverlap)
Code language:Lua(lua)
You can now add more coins, more items and here are some bonus ideas that you could implement:
This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.
Strictly Necessary Cookies
Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.
If you disable this cookie, we will not be able to save your preferences. This means that every time you visit this website you will need to enable or disable cookies again.
3rd Party Cookies
This website uses Google Analytics to collect anonymous information such as the number of visitors to the site, and the most popular pages.
Keeping this cookie enabled helps us to improve our website.
Please enable Strictly Necessary Cookies first so that we can save your preferences!