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
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.
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.
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