✓ Accepted answer
For future reference, put your code in a Lua code block.
Assuming that like you said, this is in a text button, this means this code is being executed via a local script. The thing is, the server doesn't see this change and therefore doesn't care. The best way to do this is through a remote event.
Add a RemoteEvent in ReplicatedStorage, and call it GiveGunEvent. Also put your sniper inside ServerStorage.
Now that we have our workspace set up, we can begin. Change the local script inside your gun to the following:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local giveGun = ReplicatedStorage:WaitForChild("GiveGunEvent")
local button = script.Parent
button.MouseButton1Down:Connect(function()
giveGun:FireServer()
end)
So we just simply fired the event. Now, we need to make something that listens for the RemoteEvent, and when it hears the call, give the player the gun. The good news is that by default, a remote event fired to the server passes on the player argument by default, so let's make code to listen to that now. Make a Script inside ServerScriptService and add this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local giveGun = ReplicatedStorage:WaitForChild("GiveGunEvent")
local sniper = ServerStorage:WaitForChild("Sniper")
giveGun.OnServerEvent:Connect(function(player)
local backpack = player.Backpack
local gunClone = sniper:Clone()
gunClone.Parent = backpack
end)
Hope this helped!
4 comments
make it a lua script example for you code
A code block
done
Did my answer work?