Server Script and Module problem

Hey all. So I’ve wanted to avoid using Debris and instead use task.delay in a module due to its dated nature and potential problems which might arise from it and the like, thing is, however, it will stop as soon as the character is deleted (script inside requires the module). Would I be better off sticking with debris, trying another approach, or is there some sort of an oversight I made?

function Commonalities.DebrisClear(Instance, Time)
	task.delay(Time, function()
		Instance:Destroy()
	end)
end

--Server Script requiring this below:
--[[task.spawn(function()
while task.wait(.33) and script.Parent:FindFirstChild("Humanoid").Health > 0 do if
script.Parent:FindFirstChild("Humanoid").FloorMaterial ~= Enum.Material.Air then local DamageTrail = game:GetService("ServerStorage").Assets.Trail:Clone()
DamageTrail.Touched:Connect(function(hit)
if require(game:GetService("ReplicatedStorage"):FindFirstChild("TeamRegistrations"))[game.Players:GetPlayerFromCharacter(script.Parent).Team.Name][game.Players:GetPlayerFromCharacter(hit.Parent).Team.Name] == nil then hit.Parent:FindFirstChild("Humanoid").Health -= 5
end end)
DamageTrail.CFrame = CFrame.new(script.Parent:FindFirstChild("HumanoidRootPart").CFrame.Position.X, script.Parent:FindFirstChild("HumanoidRootPart").CFrame.Position.Y - 2.501, script.Parent:FindFirstChild("HumanoidRootPart").CFrame.Position.Z)
DamageTrail.Parent = workspace--]]
require(game:GetService("ReplicatedStorage"):FindFirstChild("Functionalities")).DebrisClear(DamageTrail, 20)
end
end
end)

I mean the way you’re doing it now works fine but I’d check if the Instance is nil or not before attempting to destroy it.

function Commonalities.DebrisClear(Instance, Time)
	task.delay(Time, function()
		if Instance ~= nil then
           Instance:Destroy()
        end
	end)
end

You could however just create another script that manages Destroying Items after a certain period, for example you could have a script that you then Parent to the thing you’re trying to destroy with a IntValue to destroy it.

or could just use debris service.

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