Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

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

Asked by 4 years ago

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)
0
LocalPlayer is nil if its in a regular script and ServerStorage's children isnt visible in the regular script SoftlockedUnderZero 668 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

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.

0
Wow, thank you so much for your help. ThadonROOX 47 — 4y
0
No problem. youtubemasterWOW 2741 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
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)
0
Not working ThadonROOX 47 — 4y
0
i edited it User#29913 36 — 4y
0
Still not working and I don't have nothing in output ThadonROOX 47 — 4y
0
make sure you are using server script User#29913 36 — 4y

Answer this question