If you’re still lost, basically you need two scripts, a local script in the button, and a server script preferably in ServerScriptService. A RemoteEvent needs to be placed in ReplicatedStorage, the only place that the server and client are truly the same.
When the button is pressed, the Remote shall be fired, which will make the server script do stuff.
You are using StarterGUI, so a click wont register anyways.
even if the click was registered, this would run for the next player that joins and not the player that clicked.
So, put the script under the button and use it like this:
local button = script.Parent
local player = script:FindFirstAncestorOfClass("Player")
button.MouseButton1Click:Connect(function()
local backpack = player:WaitForChild("Backpack")
local cloneTool = game.ServerStorage.M4:Clone()
cloneTool.Parent = backpack
end)
Also, the more recommended way would be to use a remote event, a server script to handle the remote event, and a client script to handle the button. (This puts less stress on the server, even if only a bit less)
---server script
local re = --remote event
re.OnServerEvent:Connect(function(player)
local backpack = player:WaitForChild("Backpack")
local cloneTool = game.ServerStorage.M4:Clone()
cloneTool.Parent = backpack
end)
--client script (under button)
local button = script.Parent
local player = game.Players.LocalPlayer
local re = --remote event
button.MouseButton1Click:Connect(function()
re:FireServer()
end)
You’re checking for PlayerAddedafter the button is clicked. The player will already be in, so the event won’t fire. The best way would be to use a local script and then fire a RemoteEvent to the server, which will pass over the player that fired it and give them the tools.
Common mistake. This needs to be a local script and you need to use remote events which basically think of them like this: you escape the map, you tell the remote event that and then it tells the server to change the players escaped text. You need a local script because you need to get the player so you could get playergui. Everything in startergui gets cloned into playergui.
Hold on, why is the weapon in ServerStorage? Dosen’t that almost delete everything in serverstorage?
--Getting the player
game.Players.PlayerAdded:Connect(function(player)
--Getting the character
local character = player.Character or player.CharacterAdded
local button = Player.PlayerGui.ScreenGui.Play
button.MouseButton1Click:Connect(function()
local backpack = player:FindFirstChild("Backpack")
local cloneTool = game.ReplicatedStorage.Tools.M4:Clone()
cloneTool.Parent = backpack
end)
end)
Local script (put under your button or wherever you want)
local button = script.Parent
local remote = game.ReplicatedStorage.GiveTool
button.Activated:Connect(function()
remote:FireServer()
end)
Server script (put inside serverscriptservice)
local remote = game.ReplicatedStorage.GiveTool
local tool = where your tool is
remote.OnServerEvent:Connect(function(player)
tool:Clone().Parent = player.Backpack
end)
What? What do you even mean? Serverstorage stores models for future reference which is used for loading maps. You can clone the thing in serverstorage and parent it to workspace and it’ll work.
I just came back to this a few hours later, but some people are saying that it shouldn’t be in ServerStorage, that it would delete everything in ServerStorage, and that you are a novice for putting the model there, but ServerStorage is actually the correct place to put it. Putting it in ServerStorage simply means only the server can access the model, which in this case, doesn’t affect the code and is actually what is needed anyways as cloning a gun on the client usually doesn’t work. For the people saying everything in ServerStorage gets deleted, it may look like that on the client but you probably didn’t look on the server and check there. It’s called ServerStorage for a reason. Also, if it were to delete everything, what would be the point of ServerStorage even existing?