Instance is nil when it is not?

I have this instance called “EquippedValue” and it is used to check what spawn to put the NPC on. When the player joins, it turns nil when it is not.

game.Players.PlayerAdded:Connect(function(player)
	local HeroesFolder = player:WaitForChild("HeroesFolder")
	
	for i,v in ipairs(HeroesFolder:GetChildren()) do
		local equippedValue = v:FindFirstChild("EquippedValue")
		if equippedValue then
                       -- nil
		end
	end
end)

image

Maybe the script that copies the value from the template to each NPC doesn’t manage to copy all the values in time before the PlayerAdded event fires? If that’s the case try using WaitForChild with a second parameter(timeout):

--inside your loop
local timeout = 5 --5 seconds until it gives up waiting for the item to be parented
--using WaitForChild with a timeout won't break the script as it does without it
local equippedValue = v:WaitForChild("EquippedValue", timeout)
if equippedValue then
	print("Found a value")
end

Keep in mind that’s just a guess, it’s hard to provide further help due to the lack of information from your post.

Wow. It worked! My code used to work but then it stopped when I didn’t change anything.

1 Like

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