The idea of my script is to click an on screen GUI and it will put a weapon inside the players Backpack and then the GUI will remove itself. The code is inside of a Localscript and it works perfectly when I test it in 'Play' mode but in 'Online' Mode the sword will appear in the users inventory but will make no sounds and you're unable to swing the sword itself.
Here's the code:
wait(1) local player = game.Players.LocalPlayer local sw = game.ReplicatedStorage.ClassicSword function onButtonClicked() if player:FindFirstChild("Backpack", 10) then sw:Clone().Parent = player.Backpack wait(0.1) script.Parent:remove() end end script.Parent.MouseButton1Down:connect(onButtonClicked)
Any help will be appreciated to be able to stop the models from seemingly randomly becoming broken!
FindFirstChild
is a boolean, not a number. If this argument is set to true, it will look through the descendants of the object. Also, the MouseButton1Down
event runs code whilst the mouse is down on the Gui, so it may repeat the code. If you do not want this behaviour, use MouseButton1Click
.wait(1) local player = game.Players.LocalPlayer local sw = game.ReplicatedStorage.ClassicSword local function onButtonClicked() if player:FindFirstChild("Backpack") then sw:Clone().Parent = player.Backpack wait(0.1) script.Parent:Destroy() end end script.Parent.MouseButton1Click:Connect(onButtonClicked) -- you may want the server to do the cloning so it replicates :\
:remove()
and :connect()
are deprecated, use :Destroy()
and :Connect()
instead.