Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why can't :Destroy() destroy a model?

Hi everyone. I've been having an issue with the :Destroy() function which won't allow me to destroy any models.

I've been attempting to create a battle-royale style game and the main game script is supposed to remove the map after the game has finished by using the :Destroy() function, however once the game finishes the output throws out the error "Unable to :Destroy() Terrain". In an attempt to solve this, i've completely ungrouped and regroup the entire terrain model with no success, and i'm not quite sure as to what to do next. Any help is greatly appreciated.

CODE:

local s = script.Stat
local vals = game.ReplicatedStorage.vals
t = 0
while true do
	local plrs = game.Players:GetChildren()
	if #plrs >0 then
	t = 15
	repeat
		t = t-1
		s.Value = "Intermission... "..t
		wait(1)
	until t==0
	s.Value = "Game Starting!"
	wait(2)
	local mapselect = game.ReplicatedStorage.Games:GetChildren()
	local choose = math.random(1,#mapselect)
	curnum = 0
	for i =1,#mapselect do
		curnum = curnum +1
		if curnum == choose then
			mapselect[i]:Clone().Parent = workspace
			curmap = mapselect[i].Name
		end
	end
	wait(3)
	local plrs = game.Players:GetChildren()
	for i = 1,#plrs do
		local num = math.random(1,3)
		plrs[i].Character.Head.CFrame = CFrame.new(workspace.Teleports["Part"..num].Position)
		plrs[i].Character.Parent = workspace.Ingame
	end
	t=300
	repeat
		t = t-1
		s.Value = t.." seconds left!"
		wait(1)
		local ingame = workspace.Ingame:GetChildren()
	until t ==0 or vals.Winner.Value ~= "" or #ingame == 0
	if vals.Winner.Value ~= "" then
		s.Value = vals.Winner.Value.. " has won!"
		workspace[curmap]:Destroy()
		game.Players[vals.Winner.Value].leaderstats.Points.Value =game.Players[vals.Winner.Value].leaderstats.Points.Value +50
		game.Players[vals.Winner.Value].leaderstats.Wins.Value =game.Players[vals.Winner.Value].leaderstats.Wins.Value +1
		vals.Winner.Value = ""
	else
		s.Value = "No one has won!"
	end
	workspace[curmap]:Destroy()
	wait(3)
	local ingame = workspace.Ingame:GetChildren()
	for i =1,#ingame do
		local plr = game.Players:GetPlayerFromCharacter(ingame[i])
		plr:LoadCharacter()
	end
	else
		s.Value = "Two or more players are needed to play!"
		wait(1)
		end
end

2 comments

0

If you've named your map "Terrain", then you're going to have conflicts with the object used for ROBLOX's actual terrain system, which cannot be destroyed.

0

Ahh, THAT'S what it was! Thanks so much!

1 answer

✓ Accepted answer

:Destroy() is the equivelant of setting an object's parent to nil. There's no reason why it shouldn't be able to remove a model.

Why? A model inherits all functions, events, etc. from Instance, meaning :Destroy should work. The problem is most likely within your code. My suggestion would be to add print statements directly before or after attempting to :Destroy() the model.

If you want to learn more about how :Destroy works, read this article.

This discussion is preserved as it was. New answers, comments, and votes are closed.