Obtaining Tools from GUIs

When I try to click the “YES” button to obtain a tool from clicking the GUI button, the tool does not get placed into the players backpack.

The output says this:
Players.coke_cripsXD.PlayerGui.DealerGUI.ConfirmGUI.Yes.Script:5: attempt to index nil with ‘Backpack’

This is the script


yes.MouseButton1Click:Connect(function(player)
	local food = game.ReplicatedStorage.Sugar:Clone()
	food.Parent = player.Backpack
end)
3 Likes

This should had been pretty obvious, it cant find player

Maybe I didn’t reference the player properly

Try:

local players = game:GetService('Players')
local player = players.LocalPlayer

yes.MouseButton1Click:Connect(function(player)
	local food = game.ReplicatedStorage.Sugar:Clone()
	food.Parent = player.Backpack
end)

If this is a local, do

local player = game.Players.LocalPlayer

capitalized ‘local’? What? lol

1 Like

My bad im on phone and on class at the same time

Also remove the player from the function, it will cause error

It works on other things just not GUIs, the player parameter is to work with the script that you see but apparently for adding it to GUI buttons it doesnt work

I just did this.

Client script inside your Button UI

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- You have to create remote event

local food = game.ReplicatedStorage:WaitForChild("Sugar")

local yes = script.Parent

yes.Activated:Connect(function()
remote:FireServer(food)
end)

And… this is server script.

local remote = game.ReplicatedStorage:WaitForChild("RemoteEvent") -- You have to create remote event

remote.OnServerEvent:Connect(function(Plr, food)
local clone = food:Clone()
clone.Parent = Plr.Backpack

I don’t know if this is a solution.

2 Likes

This is a solution, preferably what you actually do cause you cant add a tool on local

It’s either I’m not doing it right, or it doesn’t work

Gonna try this one out see if it works

Didn’t work, maybe an idea on where I should place the local script/regular script with the remote event? Nothing is coming out in the output either which is confusing

So, youre supposed to place the local script inside the button, and if its on startergui, its gonna always be on the player(ignore this if you manually summon the gui),once the player click on it, you will use the remotevent to the server to give the player the tool
Now, to use the mouse1click event, you can put the script inside the button, and do

local button = script.parent
button.MouseButton1Click:Connect(function()
--code here (use remoteevent like remotevent:fireserver or smth im on phone)
end)

Forgot to tell you that, remotevent always send the player data if its local so you dont need any parameters for the player, only the food(sorry i was on phone and on school at that time)

@war44malk has sent an example of the script, which should work

1 Like

Also i didnt check this until now, it needs an end) on the serverscript

Got this error

Players.coke_cripsXD.PlayerGui.DealerGUI.ConfirmGUI.Yes.LocalScript:6: attempt to index number with ‘Backpack’

1 Like

This is the solution, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.