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)
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.
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)