Model keeps deleting after clone

When it clones, after like a couple seconds, it deletes.

local Lighting = game:GetService("Lighting")
local Entities = game:GetService("ReplicatedStorage"):WaitForChild("Entities")

local EntitiesModule = require(script.Entities)

local EntityTable = {
	
	Table = {

		Entities.Zombie;

	};

	Count = 100;
	Parent = workspace.Entities;
	
}

local NeutralEntityTable = {

	Table = {
		
		Entities.Pig;
		
	};

	Count = 7;
	Parent = workspace.NeutralEntities;

}

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	
	if Lighting.ClockTime >= 18.6 or Lighting.ClockTime <= 6 then
		
		EntitiesModule:AddEntities(EntityTable)
		
	else
		
		EntitiesModule:RemoveEntities()
		
	end
	
	if Lighting.ClockTime >= 6.3 or Lighting.ClockTime <= 18.6 then

		EntitiesModule:AddEntities(NeutralEntityTable)

	else

		EntitiesModule:RemoveEntities()

	end
	
end)

The only thing I can think of is “troubleshooting” your else statements, this can be achieved with the following:

local Lighting = game:GetService("Lighting")
local Entities = game:GetService("ReplicatedStorage"):WaitForChild("Entities")

local EntitiesModule = require(script.Entities)

local EntityTable = {
	
	Table = {

		Entities.Zombie;

	};

	Count = 100;
	Parent = workspace.Entities;
	
}

local NeutralEntityTable = {

	Table = {
		
		Entities.Pig;
		
	};

	Count = 7;
	Parent = workspace.NeutralEntities;

}

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	
	if Lighting.ClockTime >= 18.6 or Lighting.ClockTime <= 6 then
		
		EntitiesModule:AddEntities(EntityTable)
		
	else
		
		print("A")
		EntitiesModule:RemoveEntities()
		
	end
	
	if Lighting.ClockTime >= 6.3 or Lighting.ClockTime <= 18.6 then

		EntitiesModule:AddEntities(NeutralEntityTable)

	else

		print("B")
		EntitiesModule:RemoveEntities()

	end
	
end)

This is probably not the most efficient way to troubleshoot however it can work. Additionally it would help if you provided the module script for reference.

1 Like
function module:AddEntities(Entities)
	
	if EntitiesSpawn == false then

		EntitiesSpawn = true

		if typeof(Entities) ~= "table" then
			error("Provide a table please!")
			return
		end

		for i = 1, Entities.Count do

			local Randomize : Model = Entities.Table[math.random(1, #Entities.Table)]:Clone()
			Randomize.Parent = Entities.Parent
			Randomize:PivotTo(CFrame.new(
				math.random(-workspace.Terrain.Size.X / 1.5, workspace.Terrain.Size.X /1.5), 
				workspace.Terrain.Size.Y + 150, 
				math.random(-workspace.Terrain.Size.Z / 1.5, workspace.Terrain.Size.Z / 1.5)
				)
			)
			
			task.wait()
			
		end
		
	end
	
end



function module:RemoveEntities()
	
	workspace.Entities:ClearAllChildren()
	workspace.NeutralEntities:ClearAllChildren()
	
	EntitiesSpawn = false
	
end

1 Like

Try the “troubleshoot” code I added (two print statements within the “else” parameters) and that will point you to the cause of the deletion.

1 Like

Okay I found the solution by myself. I will mark you as the solution still. Thanks for your time helping me.

2 Likes

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