Attempt to index nil with :Destroy()

Hello! I was going through my old games and I played one of them, only to get this error:
image
Here is my code:

wait(4)

function gameStart()
	game.ReplicatedStorage.GameStart:FireAllClients()
	local map = game.ReplicatedStorage.Maps.Map1:Clone()
	map.Parent = game.Workspace
	map.Name = "Map"
	game.Workspace.Text.Value = "Game Starting In 5 Seconds!"
	wait(1)
	game.Workspace.Text.Value = "Game Starting In 4 Seconds!"
	wait(1)
	game.Workspace.Text.Value = "Game Starting In 3 Seconds!"
	wait(1)
	game.Workspace.Text.Value = "Game Starting In 2 Seconds!"
	wait(1)
	game.Workspace.Text.Value = "Game Starting In 1 Seconds!"
	wait(1)
	game.Workspace.Text.Value = "The Game Has Started!  GO!"
	game.Workspace.GameMusic:Play()
	game.Workspace.Lobby.LobbyMusic:Stop()
	game.Workspace.Barrier.CanCollide = false
	game.Workspace.Barrier.Transparency = 1
end

game.Workspace.Winner.Touched:Connect(function(otherPart)
	if otherPart.Parent:FindFirstChild("Humanoid") ~= nil then
		game.Workspace.Lobby.LobbyMusic:Play()
		game.Workspace.GameMusic:Stop()
		otherPart.Parent:MoveTo(game.Workspace.SpawnPoint.Position)
		game.ReplicatedStorage.GameEnd:FireAllClients()
		game.Workspace.Barrier.CanCollide = true
		game.Workspace.Text.Value = otherPart.Parent.Name .. "Has Won! Congrats!"
		wait(1)
		game.Workspace:FindFirstChild("Map1"):Destroy()
		game.Workspace.Text.Value = "Intermission"
		game.Workspace.Barrier.Transparency = 0.6
		wait(15)
		gameStart()
	end
end)

game.Workspace.Lobby.LobbyMusic:Play()
wait(5)
gameStart()

I know it could be cleaned up a bit, but I think it’s still pretty readable. If you think you know what wrong, please let me know. Thank you and have a wonderful day!

2 Likes

Here you’re adding “Map” to workspace:

local map = game.ReplicatedStorage.Maps.Map1:Clone()
map.Parent = game.Workspace
map.Name = "Map"

And here you’re trying to destroy “Map1”:

game.Workspace:FindFirstChild("Map1"):Destroy()

I think all you have to do is change “Map1” in the FindFirstChild call to “Map” or change the name of the map to “Map1.”

1 Like

Sadly, it doesn’t work. I still get spammed with the error.

1 Like

Perhaps you need to use WaitForChild or change the name of cloned map to “Map1”
“attempt to index nil with Destroy()” usually refer to a instance that doesn’t exist and yet you called :Destroy() function on that nil instance, Thus, creating the error.

1 Like

It worked! Thank you so much!

I knew why it was giving the error, but since I’m kind of a noob at scripting, I wasn’t sure how to fix it.

1 Like

either wait for child or the instance that you are trying to destroy does not exist.

1 Like

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