How to make model despawn at a certain time?

  1. What do you want to achieve? the enemy to despawn after 6 and before 18

  2. What is the issue? the enemy spawn YAy! but they dosent despawn Awww

  3. What solutions have you tried so far? When i look up how to despawn a part on roblox that what show up I don’t know how to implemenit into the script

local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
local ChosenNPC = NPCS[math.random(#NPCS)]:Clone()

while true do
	local time = game:GetService("Lighting").ClockTime

	if time >= 18 or time < 6 then
		local NPCS = game.ReplicatedStorage.Enemies:GetChildren()
		local ChosenNPC = NPCS[math.random(#NPCS)]:Clone()

		if ChosenNPC ~= nil then
			ChosenNPC.Parent = workspace
			if workspace.EnemySpawns ~= nil and workspace.EnemySpawns.Spawn1 ~= nil then
				ChosenNPC.PrimaryPart = ChosenNPC:FindFirstChild("PrimaryPart")
				if ChosenNPC.PrimaryPart ~= nil then
					ChosenNPC.PrimaryPart.Position = workspace.EnemySpawns.Spawn1.Position
				end 
			end
		end
		
	else
		ChosenNPC:Destroy() 
	end

	task.wait(0.3)
end

Here’s what I figure the problem is:
The reason your current script doesn’t work is because ChosenNPC is only set once outside of the if statement, so when the else block is ran, it executes the :Destroy() on that specific variable (the one outside the if statement).

Of course, I can’t tell if that is your problem or not, because you don’t provide any errors that were printed in the Output window, nor detail your problem well.

there is no error that are printer
for more detail is that it dosen’t remove the Enemys normaly when it is other than after 18 and before 6 it dosent do the action
relly sorry if a frustrated you.

No worries, it’s just a good rule of thumb to describe the error in as much detail as possible, so time isn’t wasted going back and forth.
Have you tried renaming your variable time? It’s using the name of a global variable.
Also, consider saving the service as a variable outside the while loop, and surround each condition in your if statement with brackets, like so:
if (time >= 18) or (time < 6) then
This will make sure you don’t experience logic errors, because the interpreter will read it exactly how you wanted it to.
Also, try removing the local keyword from the 2 variables inside the if statement.

YES it work Thx alot i was trying this for 4 days Thx soo mutch!!!

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