Why is Tool not working when cloned?

Hello

I am making a gui button that when clicked, gives the Player a random item.

The script works but it’s whenever I equip the tool it won’t slash (I am using the Classic Sword from Roblox). There are no errors in the output and I opened workspace because the tool was not in the players actual backpack, everytime I equip the tool, it goes into the Character instead.

I don’t know how I could fix this, but this issue never happened before, here is the local script:

local Button = script.Parent

local ItemFolder = game:GetService("ReplicatedStorage"):WaitForChild("Weapons"):GetChildren()

Button.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local Weapon = ItemFolder[math.random(1, #ItemFolder)]:Clone()
	
	Weapon.Parent = player.Backpack
	Button.Visible = false
	
	print("Button was pressed")
	
end)
2 Likes

change this to a server script and make the player variable equal to this:

local player = script:FindFirstAncestorOfClass("Player")

the problem was that cloning scripts to the player through a local script only affects the player but not the other players.

2 Likes

Lol, don’t do this.

1 Like

Once a click is detected, fire a remote to the server, and clone tools from there, make sure to add prints all around the scripts (including swird scripts) to see where everything stops working.

1 Like

Thats not how it works, each player presses the button any time, its a gui, not every player gets a tool at the same time

1 Like

You do realize server scripts work for buttons, right?

1 Like

That’s not what it does though. The player clicked the button, so they got the tool. Simple as that. It’s quite literally the same exact script you used, but redefining the player. I’m not so sure what you find “every player gets the tool” from the script.

1 Like

Please, learn to script. :sob:

1 Like

I have learned to script, though? If I am saying something wrong, you should correct it instead of saying it’s wrong or “learn to script.” You’re not going to get anywhere by just trying to insult me by saying “learn to script” even if it has a please next to it. Telling me what I did wrong and how exactly it is wrong will be a better solution. Controlling a button through a server script isn’t particularly bad. In fact, it can be used to handle many things within the game. Yes, I agree that it can be done with a remote event and that it doesn’t matter either way. Please, think about what you say before you say it.

1 Like

I thinkn @CipherFunctions is correct, it’s a pretty bad idea to run server scripts inside the local player. Are you sure your a scripter? Using remotes is the best way.

2 Likes

This is why I don’t like dealing with other people. I already said it’s fine to do it by a remote event. I just said that either way would work. And, no, it’s not a bad idea to use server scripts within the player, but it is best to limit the amount.

1 Like