Time Stop Ability Breaking when player dies in "warning stage"

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local SoundService = game:GetService("SoundService")
local Debris = game:GetService("Debris")
local Lighting = game:GetService("Lighting")

local ColorCorrection = Lighting:WaitForChild("ColorCorrection")

local TimeStopEvent = script.Parent
local ResumeTime_Sound = script:WaitForChild("ResumeTime")
local StopTime_Sound = script:WaitForChild("StopTime")

local Anchored_Parts = {}
local Scripts = {}

local Humanoid = script.Parent.Parent.Parent:WaitForChild("Humanoid")
local Cooldown = TimeStopEvent.Parent:WaitForChild("Cooldown")

local function Toggle_Scripts(character, Toggle_Value)
	for i, v in character:GetDescendants() do
		if not v:IsA("LocalScript") or not v:IsA("Script") then continue end
		v.Enabled = Toggle_Value
	end
end

local function TimeStop_Warning(Character)
	local Humanoid = Character:FindFirstChild("Humanoid")
	local Highlight = Instance.new("Highlight")
	Highlight.Parent = Character
	
	Toggle_Scripts(Character, false)
	
	Humanoid.WalkSpeed = 0
	Debris:AddItem(Highlight, 5)
	
	task.wait(3)
	Toggle_Scripts(Character, true)
	
	Humanoid.WalkSpeed = 16
end

local function ResumeTime(TimeStop_AOE)
	TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = 0}):Play()
	
	for i, Part in Anchored_Parts do
		Part.Anchored = false
	end
	
	table.clear(Anchored_Parts)
	
	for i, Loop_Script in Scripts do
		Loop_Script.Enabled = true
	end
	
	table.clear(Scripts)
	
	if TimeStop_AOE == nil then return end
	TimeStop_AOE:Destroy()
end

local function Stop_Time(HRP, TimeStop_AOE)
	local StopTime_Sound_Clone = StopTime_Sound:Clone()
	StopTime_Sound_Clone.Parent = HRP
	StopTime_Sound_Clone:Play()
	
	StopTime_Sound_Clone.Ended:Connect(function()
		for i = 10, 0, -1 do
			if i <= 0 then
				local ResumeTime_Sound_Clone = ResumeTime_Sound:Clone()
				ResumeTime_Sound_Clone.Parent = HRP
				ResumeTime_Sound_Clone:Play()
				
				ResumeTime(TimeStop_AOE)
				Debris:AddItem(ResumeTime_Sound_Clone, 2)
				Debris:AddItem(StopTime_Sound_Clone, 3)
				break
			else
				i -= 1
				task.wait(1)
			end
		end
	end)
end

local ZA_WARUDO = TimeStopEvent.OnServerEvent:Connect(function(player, HRP)
	TimeStop_Warning(HRP.Parent)
	Cooldown.Value = true
	
	local TimeStop_AOE = Instance.new("Part")
	TimeStop_AOE.Shape = Enum.PartType.Ball
	TimeStop_AOE.CastShadow = false
	TimeStop_AOE.Anchored = true
	TimeStop_AOE.CanCollide = false
	TimeStop_AOE.Position = HRP.Position
	TimeStop_AOE.Size = Vector3.new(0.1, 0.1, 0.1)
	TimeStop_AOE.Transparency = 0.85
	TimeStop_AOE.Material = Enum.Material.Neon
	TimeStop_AOE.Name = "TimeStop_AOE"
	TimeStop_AOE.Parent = game.Workspace
	
	task.spawn(function()
		TweenService:Create(TimeStop_AOE, TweenInfo.new(3, Enum.EasingStyle.Linear), {Size = Vector3.new(750, 750, 750)}):Play()
		TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = -1}):Play()
	end)

	TimeStop_AOE.Touched:Connect(function(hit)
		for i, v in hit.Parent:GetDescendants() do
			if not hit.Parent:FindFirstChild("Humanoid") then continue end
			
			if v:IsA("Part") and v.Anchored == false then
				if not v:IsDescendantOf(player.Character) then
					if v.Name == "HumanoidRootPart" then continue end
					v.Anchored = true
					table.insert(Anchored_Parts, v)
				end
			elseif v:IsA("Script") or v:IsA("LocalScript") then
				if not v:IsDescendantOf(player.Character) then
					v.Enabled = false
					table.insert(Scripts, v)
				end
			end
		end
	end)
	
	Stop_Time(HRP, TimeStop_AOE)
	
	task.wait(35)
	Cooldown.Value = false
end)

Humanoid.Died:Once(function()
	ZA_WARUDO:Disconnect()
	ResumeTime(game.Workspace:FindFirstChild("TimeStop_AOE"))
	ColorCorrection.Saturation = 0
end)

I’m trying to make an ability where the player has a “warning stage” before the ability actually works. It works perfectly but when the player dies at that warning stage, time is stopped infinitely. I tried disconnecting the event but it didn’t work. What else should I try?

Anyone know how to completely cancel a remote event’s code? :thinking:

You might want to look into things like Coroutines/task

-- example:
local stopTime = coroutine.create(function()
--code
end)
-- to begin the code
coroutine.resume(stopTime)
-- to stop the running coroutine
coroutine.close(stopTime)

There are also some modules that do the same thing with more options

Wait what coroutine? I’ve been told that task is better than it.

Anyways, I’m trying to completely cancel out the connection’s code and not the Stop_Time function

I can still try coroutines though even though I’ve hardly ever used them

This can be changed to (since LocalScript inherits Script)

local function Toggle_Scripts(character, Toggle_Value)
	for i, v in character:GetDescendants() do
		if v:IsA("Script") then
			v.Enabled = Toggle_Value
		end
	end
end

I can’t seem to replicate your issue when I create and disable/enable scripts within the character.

During the warning stage (character has highlight), reset

send the place or a test place please.

Uhhhh… You’re not going to steal my assets right?

Just add checks like in the warning section check if the player is exists or the health is 0 if not exists then return nil if u want or u can use continue if it’s in for loop

1 Like

Ok take it ig this would fix it try it

local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local Debris = game:GetService("Debris")
local Lighting = game:GetService("Lighting")

local ColorCorrection = Lighting:WaitForChild("ColorCorrection")
local TimeStopEvent = script.Parent
local ResumeTime_Sound = script:WaitForChild("ResumeTime")
local StopTime_Sound = script:WaitForChild("StopTime")
local Cooldown = TimeStopEvent.Parent:WaitForChild("Cooldown")

local Anchored_Parts = {}
local Scripts = {}

local function Toggle_Scripts(character, value)
    for _, v in pairs(character:GetDescendants()) do
        if v:IsA("Script") or v:IsA("LocalScript") then
            v.Enabled = value
        end
    end
end

local function TimeStop_Warning(character)
    local humanoid = character:FindFirstChild("Humanoid")
    local highlight = Instance.new("Highlight", character)
    Toggle_Scripts(character, false)
    humanoid.WalkSpeed = 0
    Debris:AddItem(highlight, 5)
    task.wait(3)
    Toggle_Scripts(character, true)
    humanoid.WalkSpeed = 16
end

local function ResumeTime(a)
    TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = 0}):Play()
    for _, part in pairs(Anchored_Parts) do
        part.Anchored = false
    end
    table.clear(Anchored_Parts)
    for _, loopScript in pairs(Scripts) do
        loopScript.Enabled = true
    end
    table.clear(Scripts)
    if a then
        a:Destroy()
    end
end

local function Stop_Time(hrp, a)
    local clone = StopTime_Sound:Clone()
    clone.Parent = hrp
    clone:Play()
    clone.Ended:Connect(function()
        task.delay(10, function()
            local resumeClone = ResumeTime_Sound:Clone()
            resumeClone.Parent = hrp
            resumeClone:Play()
            ResumeTime(a)
            Debris:AddItem(resumeClone, 2)
            Debris:AddItem(clone, 3)
        end)
    end)
end

TimeStopEvent.OnServerEvent:Connect(function(player, hrp)
    local character = player.Character
    if not character then return end
    local humanoid = character:FindFirstChild("Humanoid")
    local aoe
    humanoid.Died:Once(function()
        ResumeTime(aoe)
        ColorCorrection.Saturation = 0
    end)
    TimeStop_Warning(character)
    Cooldown.Value = true
    aoe = Instance.new("Part", workspace)
    aoe.Shape = Enum.PartType.Ball
    aoe.CastShadow = false
    aoe.Anchored = true
    aoe.CanCollide = false
    aoe.Position = hrp.Position
    aoe.Size = Vector3.new(0.1, 0.1, 0.1)
    aoe.Transparency = 0.85
    aoe.Material = Enum.Material.Neon
    aoe.Name = "TimeStop_AOE"
    task.spawn(function()
        TweenService:Create(aoe, TweenInfo.new(3, Enum.EasingStyle.Linear), {Size = Vector3.new(750, 750, 750)}):Play()
        TweenService:Create(ColorCorrection, TweenInfo.new(3, Enum.EasingStyle.Linear), {Saturation = -1}):Play()
    end)
    aoe.Touched:Connect(function(hit)
        local hitChar = hit.Parent
        if not hitChar:FindFirstChild("Humanoid") then return end
        for _, v in pairs(hitChar:GetDescendants()) do
            if v:IsA("Part") and not v.Anchored and not v:IsDescendantOf(character) and v.Name ~= "HumanoidRootPart" then
                v.Anchored = true
                table.insert(Anchored_Parts, v)
            elseif (v:IsA("Script") or v:IsA("LocalScript")) and not v:IsDescendantOf(character) then
                v.Enabled = false
                table.insert(Scripts, v)
            end
        end
    end)
    Stop_Time(hrp, aoe)
    task.delay(35, function()
        Cooldown.Value = false
    end)
end)

I just tested your script and it didn’t stop the remote event’s script

I’m trying to completely stop the remote event’s script if the player dies in the “warning” stage

It looks like the script is inside the character:

local Humanoid = script.Parent.Parent.Parent:WaitForChild("Humanoid")

This script will be destroyed when the character dies.

The script cannot “undo” any changes it made since it not longer exists.

Wait what? I thought the character and its descendants gets destroyed after the respawn time is done. I set the respawn time to 3 seconds and that should be plenty enough time for the script to work.

I think You can wrap the connection’s code in the coroutine

local corou = nil
event.OnServerEvent:Connect(function(...)
    corou = -- your code here
end)

-- example function to end it
local function terminateServerEventCode()
    if corou then
        coroutine.close(corou)
    end
end

Hm… Let me try that :thinking:

(yeet)

Oh nice it works! (I haven’t used coroutines in so long lol)

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