Egg Hunt: attempt to index nil with

  1. What do you want to achieve? I want my eggs to :Destroy() when they are collected. But only some are destroying and some are not.

  2. What is the issue?
    image

  3. What solutions have you tried so far? I have tried looking it up on DevForum but no luck…

LocalScript: StarterPlayer > StarterCharacterScripts

local A1 = game.Workspace.Eggs:FindFirstChild('A')
local B2 = game.Workspace.Eggs:FindFirstChild('B')
local C3 = game.Workspace.Eggs:FindFirstChild('C')
local D4 = game.Workspace.Eggs:FindFirstChild('D')
local E5 = game.Workspace.Eggs:FindFirstChild('E')
local F6 = game.Workspace.Eggs:FindFirstChild('F')
local G7 = game.Workspace.Eggs:FindFirstChild('G')
local H8 = game.Workspace.Eggs:FindFirstChild('H')
local I9 = game.Workspace.Eggs:FindFirstChild('I')
local J10 = game.Workspace.Eggs:FindFirstChild('J')

A1.ProximityPrompt.Triggered:Connect(function()
	if A1 then
		A1:Destroy()
	end
end)

B2.ProximityPrompt.Triggered:Connect(function()
	if B2 then
		B2:Destroy()
	end
end)

C3.ProximityPrompt.Triggered:Connect(function()
	if C3 then
		C3:Destroy()
	end
end)

D4.ProximityPrompt.Triggered:Connect(function()
	if D4 then
		D4:Destroy()
	end
end)

E5.ProximityPrompt.Triggered:Connect(function()
	if E5 then
		E5:Destroy()
	end
end)

F6.ProximityPrompt.Triggered:Connect(function()
	if F6 then
		F6:Destroy()
	end
end)

G7.ProximityPrompt.Triggered:Connect(function()
	if G7 then
		G7:Destroy()
	end
end)

H8.ProximityPrompt.Triggered:Connect(function()
	if H8 then
		H8:Destroy()
	end
end)

I9.ProximityPrompt.Triggered:Connect(function()
	if I9 then
		I9:Destroy()
	end
end)

J10.ProximityPrompt.Triggered:Connect(function()
	if J10 then
		J10:Destroy()
	end
end)

Try to read the error and decipher it:

“attempt to index nil with ‘ProximityPrompt’” on line 12

Go to line 12, and see what you’re trying to access with “.ProximityPrompt”. This error is telling you that it’s nil (meaning it doesn’t exist).

Post a picture of the hierarchy for game.Workspace.Eggs

Here:
image

In those scripts, I have this peace of code:

script.Parent.ProximityPrompt.Triggered:Connect(function(player)
	player.EggHunt.Eggs.Value = player.EggHunt.Eggs.Value + 1
end)

Eggs > A > ProximityPrompt

Everything looks good for the path, so you know the issue isn’t there. Try to think of other things that might be causing this. Debugging is a big part of learning to program. Try to put a print statement to print out A1 and see if the hypothesis of it being nil is correct?

Then try to think of other things that might be causing it. Could the part not be spawned in yet?

The parts don’t need to spawn, they are there from the server start.

Is this the complete path to your “A” part? Post the full hierarchy

game.Workspace.Eggs.A

yes --------------------------------

image

Everything looks fine… What exactly do you mean by “But only some are destroying and some are not.”. I put your script into a place and it works perfectly fine for me.
egg.rbxl (37.6 KB)

Wait, is it because I am in studio?

So when I die, it stops destroying the eggs. But when I keep on going, it does not destroy them.

How can I fix that… -------------------

Wait, I could use CFrame. -------------

Most likely because the
Script ran before the proximity prompt instance is replicated to the client. You should do :WaitForChild() on them.

Nope, that’s not it… I might use CFrame instead of using kill

I’m talking about your proximity prompt happening to be nil problem.

That nil problem happens when a player die and try to get the eggs

Yep, that was the issueeeeeeeeeeeeeee

This might be because prior the player’s death, they already collected some eggs. When their character resets or dies, the local script within it will clone itself into the new respawned character model and rerun the same script again. When the new script tries to get proximity prompts of the eggs the player has collected, it would result in nil. That’s why.