✓ 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.
1 comment
LocalPlayer is nil if its in a regular script and ServerStorage's children isnt visible in the regular script