Attempt to index nil with 'Backpack'

I’m trying to create a grill that cooks patties and after a wait(3) I want the tool (patty) to be cloned in the players backpack.

This is what I have so far:

script.Parent.Touched:Connect(function(food,player)
	if food.Parent.Name == "Patty" then
		food.Parent.Parent = game.ReplicatedStorage
		script.Parent.Parent.Patty.Transparency = 0
		food.Parent.Name = "Cooked Patty"
		food.Parent.Patty.BrickColor = BrickColor.new("Brown")
		script.Parent.Sound:Play()
		wait(3)
		local tool = game:GetService("ServerStorage").Patty
		local toolclone = tool:Clone()
		toolclone.Parent = player.Backpack
	end
end)

The error I am getting in the Output is:

Workspace.Grill.Activate.Script:11: attempt to index nil with ‘Backpack’

I know I might have made a silly mistake here, but I’ve been trying to find a fix for about an hour, even though there are similar posts it seems there’s no similar ones to my specific case.

Thanks.

2 Likes

I think the player argument can’t be defined when using a Touched function, instead you’d need to define the Character first to get the player’s backpack, and also consider adding a debounce as well:

local debounce = false
script.Parent.Touched:Connect(function(Hit)
    if debounce == false then
        debounce = true
	    wait(3)
        local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
        if Player then
            local tool = game:GetService("ServerStorage").Patty
	        local toolclone = tool:Clone()
	        toolclone.Parent = Player.Backpack
        end
        debounce = false
    end
end)

Apologies, I didn’t include the whole script:

script.Parent.Touched:Connect(function(food,player)
	if food.Parent.Name == "Patty" then
		food.Parent.Parent = game.ReplicatedStorage
		script.Parent.Parent.Patty.Transparency = 0
		food.Parent.Name = "Cooked Patty"
		food.Parent.Patty.BrickColor = BrickColor.new("Brown")
		script.Parent.Sound:Play()
		wait(3)
		local tool = game:GetService("ServerStorage").Patty
		local toolclone = tool:Clone()
		toolclone.Parent = player.Backpack
	end
end)

I’m not sure if the debounce script would work? Because I need to define food and player as well as Hit.

Still though I’m pretty sure you can’t define it like that, as the Touched function will only find the part that it currently touched & if you want it to find the Player’s Backpack, you’d need to first define/identify it as one using local Player = game.Players:GetPlayerFromCharacter(food.Parent) (Maybe lol)

So it’s the food (patty) that I want activate Touched, not the player. Then after the food touches the part the tool from server storage is cloned into the player’s backpack.

.Touched only provides 1 variable which is the touch part, it does not provide the player.

You need to define the player, you haven’t done it

To get the player who fired the touch event you’ll want something like this:


local function OnTouched(hit)
   local Humanoid = hit.Parent:FindFirstChild(“Humanoid”)
   if Humanoid then
      local Player = game:GetService(“Players”):GetPlayerFromCharacter(Humanoid.Parent)
   end
end

Part.Touched:Connect(OnTouched)

You don’t need to do that. In fact, you’re using more characters than needed. Have a look:

local function OnTouched(Touched)
    local Player = game:GetPlayerFromCharacter(Touched.Parent)

    if Player then
        -- Execute the needed.
    end
end

PATH_TO_PART.Touched:Connect(OnTouched)
1 Like

instead of putting it in serverstorage put on in ReplicatedStorage and see of it works

Still get the nil error:

Workspace.Grill.Activate.Script:11: attempt to index nil with ‘Backpack’

I forgot that it wouldn’t throw an error if you tried to get the player of a model without a humanoid in it. My bad.

The only issue I have is:

Workspace.Grill.Activate.Script:11: attempt to index nil with ‘Backpack’

I don’t need a new touched script, if that’s what everyone is posting.

You do. The second argument of a Touched event is not the player…

From any of these I haven’t got it to work.

Could you post your updated script?

script.Parent.Touched:Connect(function(food)
	if food.Parent.Name == "Patty" then
		local player = game:GetService("Players"):GetPlayerFromCharacter(food.Parent.Parent)
		food.Parent.Parent = game.ReplicatedStorage
		script.Parent.Parent.Patty.Transparency = 0
		food.Parent.Name = "Cooked Patty"
		food.Parent.Patty.BrickColor = BrickColor.new("Brown")
		script.Parent.Sound:Play()
		wait(3)
		local tool = game:GetService("ServerStorage").Patty
		local toolclone = tool:Clone()
		toolclone.Parent = player.Backpack
	end
end)
2 Likes

This works! Thanks very much :smiley:

1 Like