Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

attempt to index nil with 'Backpack' any idea why this happens?

script.Parent.Triggered:Connect(function(players)
	players.PlayerGui.ItemGui.Enabled = true
	local gui = players.PlayerGui.ItemGui
	gui.Frame.Username.Text = "None"
	script.Parent.Parent.Parent.TouchPart.Touched:Connect(function(hit)
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			gui.Frame.Username.Text = player.Name
		end
		print(player)
		local backpack = player.Backpack --the problem is this line
		if backpack:GetChildren() then
			for i,v in ipairs(backpack:GetChildren()) do
				local guil = Instance.new("TextLabel")
				guil.AnchorPoint = Vector2.new(0.5, 0.5)
				local size = UDim2.new(0.5, 0, 0.05, 0)
				guil.Size = size
				guil.Name = v.Name
				guil.Text = v.Name
				guil.BackgroundTransparency = 1
				guil.Font = Enum.Font.Gotham
				guil.TextSize = 24
				guil.TextWrapped = true
				guil.Parent = gui.ScrollingFrame.List

			end
		end
	end)
	script.Parent.Parent.Parent.TouchPart.TouchEnded:Connect(function(hit)
		local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
		if player then
			gui.Frame.Username.Text = "None"
		end
	end)
end)

im trying to make a item scanner but it doesnt work and gave me this error any idea why and how can i fix it?

2 answers

if player then
	gui.Frame.Username.Text = player.Name 
end

change that to:

if player then
	gui.Frame.Username.Text = player.Name
else
	return
end

Try changing

local backpack = player.Backpack

to

local backpack = player:WaitForChild("Backpack")

1 comment

0

What if the player is nil?

This discussion is preserved as it was. New answers, comments, and votes are closed.