When Destroying Part output says: attempt to index nil with 'Destroy'

  1. What do you want to achieve? Keep it simple and clear!

I’m trying to make a round system that clones a part into workspace from replicatedstorage and then destroy it but it only works some of the times I test it.

  1. What is the issue? Include screenshots / videos if possible!
    It says this:
    Screenshot 2023-10-23 200758

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I have changed up the script multiple times but it keeps indexing nil with destory, I have looked for many solutions and I’ve tried many different ideas but it doesn’t seem to work.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

  • This is my code so far, it probably isn’t the most optimal or organized piece of code but it seems to work sometimes. (which has me confused). My goal is to clone a part from ReplicatedStorage to Workspace and then destroy it
-- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

-- variables
local requiredPlayers = 1

local roundlength = 30
local intermissionLenth = 15
local startinglenth = 5
local number = math.random(1,3)
local map = ReplicatedStorage.Maps:GetChildren()
local number = math.random(1, #map)


local statusText = ""
local statusUpdate = ReplicatedStorage:WaitForChild("StatusUpdate")
local waitpart = game.Workspace.Waitpart

local function waitforplayers()
	
	statusText = "Waiting For Players..."
	
	repeat 
		task.wait(0.5)
		statusUpdate:FireAllClients(statusText)
	until
	
	#Players:GetPlayers() >= requiredPlayers
	
end

local function startingRound()
	local newmap = map[number]
	 newmap:Clone()
	newmap.Parent = workspace
			
	--MapToClone.Parent = workspace
	
	print("Map Found!")

	for i = startinglenth, 0, -1 do
		statusText = "Starting Next Round...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
		end
end

local function startRound()
	
	waitpart.CanCollide = false
	
end

local function endRound()
	waitpart.CanCollide = true
	--local number = math.random(1,3)
	--local MapToDestroy = workspace:FindFirstChild("Map"..number)
	
	local newmap = game.Workspace:FindFirstChild(map)
	
		newmap:Destroy()

	print("Map Gone!")

end

while true do 
	-- Intermission
	for i = intermissionLenth, 0, -1 do
		statusText = "Waiting For Next Round...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
		
	end
	
	waitforplayers()
	startingRound()
	startRound()
	
	--GameRound
	for i = roundlength, 0, -1 do
		statusText = "Round In Progress...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
	end
	
	endRound()
	
end




3 Likes

This probably means that the part doesn’t exist, or isn’t in the path you gave it.

1 Like

Your problem might be here:

If a child of workspace with the name of map is not found, :FindFirstChild() will return nil. Try adding an if statement that checks if newmap was found.

It looks like map is actually a table, make sure you’re putting a string into :FindFirstChild().

2 Likes

Thanks for the help, but now it doesn’t destroy the part and it doesn’t say anything in the output. It just goes on to say “waiting for next round”.

Try up top … local newmap = nil
And you may as well use :WatiForChild() vs FindFirstChild()

Making newmap local to the entire program will automatically pick that up when you
newmap:Clone()

Also it looks like WizardAnt is right on the mark.

Would you mind showing your updated code, as well as what your workspace looks like?

This is my updated code, It gets the map and then gets rid of it but the new map isn’t random (It clones the exact same map)

 -- services
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

-- variables
local requiredPlayers = 1

local roundlength = 30
local intermissionLenth = 15
local startinglenth = 5
--local number = math.random(1,3)
local map = ReplicatedStorage.Maps:GetChildren()
local number = math.random(1, #map)
local newmap = map[number]

local statusText = ""
local statusUpdate = ReplicatedStorage:WaitForChild("StatusUpdate")
local waitpart = game.Workspace.Waitpart

local function waitforplayers()
	
	statusText = "Waiting For Players..."
	
	repeat 
		task.wait(0.5)
		statusUpdate:FireAllClients(statusText)
	until
	
	#Players:GetPlayers() >= requiredPlayers
	
end

local function startingRound()
	
	 newmap:Clone()
	newmap.Parent = workspace
	
	
	print("Map Found!")

	for i = startinglenth, 0, -1 do
		statusText = "Starting Next Round...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
		end
end

local function startRound()
	
	waitpart.CanCollide = false
	
end

local function endRound()
	waitpart.CanCollide = true
	
	if newmap.Parent == game.Workspace then
		newmap.Parent = nil
		
	print("Map Gone!")
end
end

while true do 
	-- Intermission
	for i = intermissionLenth, 0, -1 do
		statusText = "Waiting For Next Round...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
		
	end
	
	waitforplayers()
	startingRound()
	startRound()
	
	--GameRound
	for i = roundlength, 0, -1 do
		statusText = "Round In Progress...("..tostring(i)..")"
		statusUpdate:FireAllClients(statusText)
		task.wait(1)
	end
	
	endRound()
	
end

I hope this fixes your problem.
(No, math.random() only returns once and the number variable stays there forever.)