So I have this GUI in my game and it's supposed to give you a sword every time you click it. I have the script, but it doesn't work and I don't know what to do about it. Any suggestions? (I'm a beginner)
Here is the code:
sword = game.Lighting.Sword:Clone() script.Parent.MouseButton1Down:connect(function() local Players = game.Players:GetChildren() sword.Parent = game.Players.LocalPlayer.Backpack end)
Your script works fine in studio but you need to get the specific player so it adds it to the player's specific backpack.
local player = game.Players.LocalPlayer -- Get the LocalPlayer bp = player:WaitForChild("Backpack") -- Wait for the Backpack. lighting = game:GetService("Lighting") -- Wait for the Lighting Service sword = lighting:WaitForChild("Sword") -- // Wait for the sword to load. script.Parent.MouseButton1Down:connect(function() print("Clicked the button!") local cloned = sword:Clone() -- Clone the Sword from Lighting. cloned.Parent = bp -- Parent it to the Player's backpack. end)