Attempt to index nil with 'parent'

Helo, I am making a voting system and I’ve got it down, except, when it comes to moving the chosen map to the game, I get an error saying: Attempt to index nil with “Parent”. Can someone help? Here is my current code:

local ClonedMap = game.ServerStorage:FindFirstChild(ChosenMap)
	if ClonedMap == "Radioactive" then
		ClonedMap:PivotTo(CFrame.new(490.263, 79.457, -4.343) * CFrame.Angles(0, 0, math.pi))
	elseif ClonedMap == "Construction" then
		ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
	end
	ClonedMap.Parent = workspace --Error on this line

Thanks!

try change FindFirstChild to WaitForChild

Did you try to print the “ClonedMap” variable to check if it is really exist?

Does the same thing except there are no errors now.

It exists:
image

that doesn’t mean it exists, you can do

nil.Parent

and you will get the same results

As @shadowmaster940 suggests, the error occurs when the FindFirstChild call in the first line doesn’t find anything whose name matches ChosenMap. You can check for this by adding

if (ClonedMap ~= nil) then

right afterwards.

Also, the if statements you have will always fall through since comparing an instance to a string will always be false.

1 Like

But even with all these checks, how would I fix my problem? It still says attempt to index nil with “parent”

Hmm, I assume this script is meant for choosing a map for some kind of round-based game. What happens to the map when it is no longer needed? Does it get put back into ServerStorage or does :Destroy() get called on it?

If it’s being destroyed then that might be a cause of the issue, since in your script you are not actually making a copy of the map, only moving the map from ServerStorage to Workspace. If it gets selected again after that, there is no longer a map to move into workspace again and the call to FindFirstChild will return nil.

I destroy it, but when it comes to choosing it again I :Clone() it. Also it appears when it is the first round of the game so previous rounds wouldnt have affected it.

@blokav are you there? Sorry for the ping but this is my friends game and he is really bothering me to get this done.

maybe do local ClonedMap = game.ServerStorage:FindFirstChild(ChosenMap):Clone()…?

What is the value of ChosenMap?

What do you mean?

sadsaddasdasadsdasdasadsdsa

What is the value of ChosenMap? Have you tried printing it?

ChosenMap is an instance.

dssdffffffffffffffffffffff

There’s your issue.

The FindFirstChild() method takes a string as a parameter. Try:

local foo = game.ServerStorage:FindFirstChild(ChosenMap.Name)

You need to actually clone the map:

game.ServerStorage:FindFirstChild(ChosenMap)game.ServerStorage:FindFirstChild(ChosenMap):Clone()

Otherwise it’ll only switch to that map one time.

2 Likes
local ClonedMap = game.ServerStorage:FindFirstChild(ChosenMap.Name):Clone()

if ClonedMap.Name == "Radioactive" then
	ClonedMap:PivotTo(CFrame.new(490.263, 79.457, -4.343) * CFrame.Angles(0, 0, math.pi))
else if ClonedMap.Name == "Construction" then
	ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
end

ClonedMap.Parent = game.Workspace

Now it says attempt to index nil with :Clone()