Serverscript attempt to index nil with 'Backpack'

I’m trying to get a player backpack to give a player tools but I got stuck

you have a ToolName variable that is not a arguement of your function.

1 Like

Could you show us the entire script?

Also try replacing
print("Called") with print(player) and tell us what that prints.

If you can’t index Backpack from the player then I’m pretty certain your not passing a player object into ur function

1 Like

Tool name variable is above the function

Are you sure your passing the player through your function?

Also the “Item” variable would be nil since it’s checking if a object inside ToolFolder’s name is nil (or it would error for that part im on mobile at the moment), if you don’t have ToolName defined.

1 Like

send the whole script in text. not a screenshot as we can then modify it on our own.

1 Like
local ToolName = ""
local ToolPrice = 0


function PurchaseItem(player)
	print(player)
	local Item = nil
	local ToolFolderChildern = ToolFolder:GetChildren()

	for i, v in ipairs(ToolFolderChildern) do
		if v.Name == ToolName then
			Item = v:Clone()
		end
	end
	local ItemTest = game.ServerStorage.ToolsFolder.Machete
	local playerBackpackTool = player.Backpack:FindFirstChild(ItemTest.Name) or player.Character:FindFirstChild(ItemTest.Name)
	if not playerBackpackTool then
		Audio.Button_c1:Play()
		ItemTest.Parent = player.Backpack
	end
end
script.Parent.ItemFrame.ItemBuy.MouseButton1Click:Connect(function()
		PurchaseItem()
	end
end)

dont worry about that nil part I do have a tool name defined

You aren’t passing the player, add another argument which is the player who’s purchasing the item.
If it’s a LocalScript then use LocalPlayer.

1 Like

its not in a local script its a server script

Then try using:

script:FindFirstAncestorOfClass('Player')
1 Like

whats that suppose to be use for?
is this suppose to be used for a argument

Just so you know, you really shouldn’t be managing UI using a server script.
Try looking into remote events later.

Yep, it get’s the player. It should work if your script is descendant of a player.
Also I wouldn’t really recommend handling gui’s on the server.

where is the script located in?
Edit: Pretty sure its inside of the gui button

1 Like

it’s in a GUI and not a button I have completely rewritten the code to make the client script fire a remote function and just work the script there. so it’s solved I guessed. thanks for everyone help

1 Like