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

My script which giving tool to the player not working, how can I fix this?

Hello! I made small script which giving tool to the player when he just clicks TextButton but it's not working, how can I fix this?

this is this script

local Tool = game:GetService("ServerStorage").RPK
local Player = game.Players.LocalPlayer
local Backpack = Player:FindFirstChild("Backpack")

script.Parent.MouseButton1Click:connect(function()
    Tool:Clone().Parent = Backpack
end)

1 comment

0

LocalPlayer is nil if its in a regular script and ServerStorage's children isnt visible in the regular script

2 answers

✓ Accepted answer

You must use a RemoteEvent as the client (LocalScript(s)) cannot access ServerStorage and the client cannot just give itself tools. To fix this, insert a RemoteEvent into ReplicatedStorage and name it "GiveTool". Now change the LocalScript's code to this

local ReplicatedStorage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:Connect(function()
	ReplicatedStorage.GiveTool:FireServer()
end)

This will fire the RemoteEvent when you click the button.

Now insert a ServerScript (aka. Normal Script) into ServerScriptService

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tool = game:GetService("ServerStorage").RPK

ReplicatedStorage.GiveTool.OnServerEvent:Connect(function(player)
	Tool:Clone().Parent = player.Backpack
end)

Now the OnServerEvent RBXScriptSignal will fire when the RemoteEvent is fired, in this case, when the button is clicked. Then the script will clone the tool and place it in the player's backpack. Hopefully, this helped you! For more information on RemoteEvents and RemoteFunctions, go here.

2 comments

0

Wow, thank you so much for your help.

0

No problem.

local Tool = game:GetService("ServerStorage").RPK
local player = script.Parent.Parent.Parent
-- local player = script.Parent.Parent.Parent.Parent
--only do that if in another thing

script.Parent.MouseButton1Click:Connect(function()
    Tool:Clone().Parent = player:WaitForChild("Backpack")
end)

4 comments

0

Not working

0

i edited it

0

Still not working and I don't have nothing in output

0

make sure you are using server script

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