Hello, i have this issue where when a player(only that player) clicks a button, they recieve a tool.
The problem is it does not give the tool and i get this error:
Players.wanu10.PlayerGui.ScreenGui2.TextButton.Script:7: attempt to index nil with ‘Backpack’
here is my code:
local tool = game.ServerStorage.Tool
script.Parent.MouseButton1Click:Connect(function(plr)
local Tool1 = tool:Clone()
print(plr)
Tool1.Parent = plr
end)
What you should do instead is you should detect the MouseButton1Click event on the client - local script and then send a remote event onto the server where it would give them the tool
--local script
-- script parented to the button
local remote = game.ReplicatedStorage.RemoteEvent -- using remote event; you would need to add this
script.Parent.MouseButton1Click:Connect(function()
remote:FireServer()
end)
-- server script (ServerScriptService)
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(player)
local tool = game.ServerStorage.Tool:Clone()
tool.Parent = player.Backpack
end)
i do have a question tho, now that that works i tried to make it so when you click it a certain value goes away.
for now i need a value to be - 3 when crafted.
here is the code you gave me and i modified it.
local remote = game.ReplicatedStorage.RemoteEvent
remote.OnServerEvent:Connect(function(player, plr)
print(plr)
local wood = plr.leaderstats.Wood
local tool = game.ServerStorage.Tool:Clone()
tool.Parent = player.Backpack
wood.Value = wood.Value - 3
end)