Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

the gun is not given on button press?

local p = game.Players.LocalPlayer

script.Parent.MouseButton1Click:connect(function()
	local tool = script.Parent.Sniper:Clone()
	tool.Parent = p.Backpack 
end)

i have txt button and inside is the gun named (Sniper) and the script above no clue why its not working thanks in advance

4 comments

0

make it a lua script example for you code

0

A code block

0

done

0

Did my answer work?

1 answer

✓ 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:

--Get the ReplicatedStorage service
local ReplicatedStorage = game:GetService("ReplicatedStorage")

--Refrence objects we need for later
local giveGun = ReplicatedStorage:WaitForChild("GiveGunEvent")
local button = script.Parent

--The following code will activate when the button is clicked
button.MouseButton1Down:Connect(function()
	--Fire the remote event
	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:

--Get ReplicatedStorage and ServerStorage
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

--Get the remote event object and the gun object from ServerStorage
local giveGun = ReplicatedStorage:WaitForChild("GiveGunEvent")
local sniper = ServerStorage:WaitForChild("Sniper")

--When the event is fired
giveGun.OnServerEvent:Connect(function(player)
	local backpack = player.Backpack
	local gunClone = sniper:Clone()

	gunClone.Parent = backpack
end)

Hope this helped!

1 comment

0

hi sorry had a brake lastnight i done that and got this error ContentProvider:Preload() failed for rbxassetid://138499093

This discussion is preserved as it was. New answers, comments, and votes are closed.