Good day,
i make a Game in Core,
i actually make a ingame-Shop the UI usw. is ready but in my Button pressed function for buy the equipment i have a problem. I dont know and i didnt can find anything in the Web how to equip items in a script that means z.b i have a item or a Template and want to give it the player in his Hand is there a specific function for that?
Thanks to all in advance!
I finnaly get it,
if sombody need is here is my Code:
local Weapon = script:GetCustomProperty("Axe_2")
local data = script:GetCustomProperty("Luberjackdata")
local Trigger = script.parent
function BoughtItem(trigger, player)
local storage = Storage.GetPlayerData(player)
local sharedStorage = Storage.GetSharedPlayerData(data, player)
local Coins = sharedStorage["Coins"] or storage["Coins"]
local actuallAxe = sharedStorage["actuallAxe"] or storage["actuallAxe"]
local Minus = nil
Minus = Coins -1
if actuallAxe == 1 then --SharedData
if Coins >= 1 then
Picked = 1
player.SetResource(player, "Coins", Minus)
player.SetResource(player, "actuallAxe", 2)
local weapon = World.SpawnAsset(Weapon, {})
for _, equipment in ipairs(player:GetEquipment()) do
equipment:Unequip()
weapon:Equip(player)
end
end
end
end
Trigger.interactedEvent:Connect(BoughtItem)
You can just spawn and the call equip on the item.
It could look something like this (haven't tested this specific script, but I have done something similar before).
For simplicity I am just using a trigger that you interact with. Replace with UI button as needed.
-- Pass trigger and weapon template in as Custom Properties.
local Weapon = script:GetCustomProperty("Weapon"):WaitForObject() -- Fill in with what shows up when you dragged the template here
local Trigger = script:GetCustomProperty("Trigger"):WaitForObject() -- fill in with trigger info
function BoughtItem(trigger, player) --Some player triggers this
local weapon = World.SpawnAsset(Weapon, {<options here if needed>})
weapon:Equip(player) -- template should already specify which socket it goes to.
end
Trigger.onInteractedEvent:Connect(BoughtItem)
Thanks for response on aa Trigger all work well, but i have it on a button and there it doesent work(I think its because its in a client context)! When i click the button this error appear:
"
Error running Lua task: [5EB0F315C80A24D1] BUY_1:7: Player value in Equipment:Equip() was invalid.
"
local Weapon = script:GetCustomProperty("Axe")
local Trigger = script.parent
function BoughtItem(trigger, player) --Some player triggers this
print("Work")
local weapon = World.SpawnAsset(Weapon, {})
weapon:Equip(player) -- template should already specify which socket it goes to.
end
Trigger.clickedEvent:Connect(BoughtItem)
I took a quick look, and clickedEvent is for UIButton which passes in just the button.
I ran a quick test by having two clients running in preview and pressing the button.
It only fired off once in the local client.
Rename Trigger to Button, Update BoughtItem to something like this
function BoughtItem(button)
local player = Game.GetLocalPlayer()
local weapon = World.SpawnAsset(Weapon)
weapon:Equip(player)
end
One caveat is that when I equipped the weapon this way, it did not show up on another player's screen.
You might have to do some of the weapon pickup script logic here.
Ok, thanks i forget to post but i pased to find this out my selv. But i still have one Problem because it equip 2Axes when i buy the next because i dont unequip the axe but my problem is that i dont know what i pass in the Uneqip function. Momenty i try to find it in the Core API.
If the player local client pressed the button to buy weapons then you send to the server Events.Broadcast value of the weapon or template and the server checks whether the player has money if yes then remove his part of the money and spam template without a server and a clean script without a server bracket otherwise the weapon will be struck in the server without a client.
But you can do 2 scripts 1 server and another standard one where the server is also responsible but for the network object defeat...
The server tells the other script to create a weapon for the player and the script created a template weapon and he became a network object and dress him immediately. All clients will see who has the weapon.
Ok, tahnsk but i dont get it how to unEquip the old axe!
(My code:)
local Weapon = script:GetCustomProperty("Axe_2")
local data = script:GetCustomProperty("Luberjackdata")
local Trigger = script.parent
local Picked = 0
function BoughtItem(trigger, player)
local storage = Storage.GetPlayerData(player)
local sharedStorage = Storage.GetSharedPlayerData(data, player)
local Coins = sharedStorage["Coins"] or storage["Coins"]
local actuallAxe = sharedStorage["actuallAxe"] or storage["actuallAxe"]
local Minus = nil
Minus = Coins -1
if Picked <= 5 then
if Coins >= 1 then
Picked = 1
player.SetResource(player, "Coins", Minus)
player.SetResource(player, "actuallAxe", 2)
local weapon = World.SpawnAsset(Weapon, {})
weapon:Equip(player)
end
end
end
Trigger.interactedEvent:Connect(BoughtItem)