Is there a way to keep a script running even if it has been destroyed?

I am making a system that lets you use abilities when you have certain tools, only problem is that when you use the ability after you die right before you respawn, the script will get destroyed, since the tool gets destroyed, making the ability effects stay there forever

local cd1 = false
local cd2 = false
local ts = game:GetService("TweenService")

script.Parent.AbilityEvent.OnServerEvent:Connect(function(plr,ability)
	if ability == "Death" then
		local char = plr.Character
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {char}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		local direction = Vector3.new(0,-50000000000,0)
		local hrp = char:WaitForChild("HumanoidRootPart")
		local origin = hrp.Position
		local result = game.Workspace:Raycast(origin,direction,raycastParams)
		local rootPoint = result.Position

		local part = Instance.new("Part")
		part.BrickColor = BrickColor.new("Teal")
		part.Size =	Vector3.new(0,.25,0)
		part.Anchored = true
		part.Position = rootPoint

		part.Parent = workspace
		ts:Create(part,TweenInfo.new(2), {Size = Vector3.new(160,.25,160)}):Play() wait(2)
		local Script = script.Parent.OtherScriptCusISuckAtCoding:Clone()
		Script.Parent = part
		Script.Enabled = true
		
		local sound = script.Parent.DeathAbilitySound:Clone()
		sound.Parent = part
		sound:Play()
		task.spawn(function()
			wait(4)
			sound:Destroy()
		end)
		
		for i = 1, 60 do
			local spike = game.ServerStorage.AbilityObjects.IceSpike:Clone()
			local Script = script.Parent.SpikeScript:Clone()
			Script.Parent = spike
			Script.Enabled = true
			local newval = math.random(25,35)
			spike.Size = Vector3.new(newval,math.random(30,60),newval)
			spike.Position = rootPoint + Vector3.new(math	.random(-80,80),-31,math.random(-80,80))
			spike.Parent = workspace
			ts:Create(spike, TweenInfo.new(1), {Position = spike.Position + Vector3.new(0,31,0)}):Play()
		end
	end
	if ability == "Q" then
		if cd1 == false then
			cd1 = true
			local sound = script.Parent.IcePlaneSound:Clone()
			sound.Parent = script.Parent.Handle
			sound:Destroy()
			local char = script.Parent.Parent
			local part = Instance.new("Part")
			
			part.Touched:Connect(function(hit)
				if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= char then
					if game.Players:GetPlayerFromCharacter(hit.Parent).Team ~= plr.Team or game.Players:GetPlayerFromCharacter(hit.Parent).Team == nil then
						hit.Parent.Humanoid.WalkSpeed = 0
						wait(6)
						hit.Parent.Humanoid.WalkSpeed = hit.Parent.DefaultSpeed.Value
					end
				end
		end)
			
			part.BrickColor = BrickColor.new("Teal")
			part.Transparency = 0.5
			part.Anchored = true
			part.Size = Vector3.new(0,.25,0)
			local raycastParams = RaycastParams.new()
			raycastParams.FilterDescendantsInstances = {char}
			raycastParams.FilterType = Enum.RaycastFilterType.Exclude
			local direction = Vector3.new(0,-50000000000,0)
			local hrp = char:WaitForChild("HumanoidRootPart")
			local origin = hrp.Position
			local result = game.Workspace:Raycast(origin,direction,raycastParams)
			
			part.Position = result.Position
			part.Parent = workspace
			ts:Create(part, TweenInfo.new(3), {Size = Vector3.new(40,.25,40)}):Play() wait(3)
			
			task.spawn(function()
				task.wait(1)
			ts:Create(part, TweenInfo.new(2), {Size = Vector3.new(0,.25,0)}):Play() wait(2)
			part:Destroy()
			print("why")
			end)
			
			wait(1)
			cd1 = false
		end
	else
		if ability == "E" then
			if cd2 == false then
				cd2 = true
				
				task.spawn(function()
					wait(2)
					cd2 = false
				end)
				task.spawn(function()
				local sound = script.Parent.IceSpikeStartAbility:Clone()
				sound.Parent = script.Parent.Parent.HumanoidRootPart
				sound:Destroy()
				wait(2)
				end)
				local spike = game.ServerStorage.AbilityObjects.IceSpike:Clone()				
				local char = plr.Character or plr.CharacterAdded:Wait()
				
				spike.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= char then
						if game.Players:GetPlayerFromCharacter(hit.Parent).Team ~= plr.Team or game.Players:GetPlayerFromCharacter(hit.Parent).Team == nil then
						hit.Parent.Humanoid:TakeDamage(10)
						end
					end
				end)
				
				local direction = Vector3.new(0,-50000000000,0)
				local hrp = char:WaitForChild("HumanoidRootPart")
				local origin = hrp.Position + hrp.CFrame.LookVector * 15
				local result = game.Workspace:Raycast(origin,direction)
				
				if  result then
				spike.Position = result.Position - Vector3.new(0,13,0)
				spike.Parent = workspace
				ts:Create(spike, TweenInfo.new(1), {Position = spike.Position + Vector3.new(0,20,0)}):Play()
				wait(2)
				ts:Create(spike, TweenInfo.new(5), {Size = Vector3.new(0,0,0)}):Play()
				ts:Create(spike, TweenInfo.new(5), {Position = spike.Position - Vector3.new(0,10,0)}):Play()
				ts:Create(spike, TweenInfo.new(5), {Transparency = 1}):Play()
				wait(5)
				spike:Destroy()
				end
			end
			end
		end
	end)

You can do: game:GetService("Debris"):AddItem(part, 5) to make a instance be destroyed after a set amount of time even after the script it destroyed.
Also don’t do:

task.spawn(function()
	task.wait(1)
     -- code
end)

Do:

task.delay(1, function()
  -- code
end)

This just returned attempt to index nil with 'touched'

i changed

local spike = game.ServerStorage.AbilityObjects.IceSpike:Clone

to

local spike = game:GetService("Debris"):AddItem(game.ServerStorage.AbilityObjects.IceSpike:Clone(),7)

did i do this wrong

Yes you did it wrong. do this instead:

local spike = game.ServerStorage.AbilityObjects.IceSpike:Clone
game:GetService("Debris"):AddItem(spike, 7)

I reccomend to take a look at this:

You need to reference spike as an Instance then set the DebrisService.

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