Problem with Clicking Gui for item

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)
2 Likes

what type of script is this because if it is a local script here is a few problems

its a normal script i used for.

also the MouseButton1Click event doesn’t have a player has an argument

what do you mean im new to coding.

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

uhmmmm what’s a client? do you mean network client?

oh so basically you can’t do

script.Parent.MouseButton1Click:Connect(function(player)
end)
-- the player would be nil

client i meaning like a local script

so what can i do? . ---------------

what should i put in the local script???

sorry im very new to scripting.

do i just type what i typed before and then send something to the server?

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

you would need to do something like this. You are new to scripting so you probably don’t understand remote events so here a link that can help u

1 Like

thanks so much. have a good day.

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)

urr i see some problems

remote.OnServerEvent:Connect(function(player, plr) -- you don't need (plr) because u got (player)
end)
local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(plr)
	print(plr)
	local wood = plr.leaderstats.Wood
	local tool = game.ServerStorage.Tool:Clone()

	tool.Parent = plr.Backpack
	wood.Value = wood.Value - 3
end)
1 Like

omg this helped tysm 1!!!