How to stop remote event when the player is dead?

Hello there!
I made a tool that can pause time, however, when players die, they can pause time again.
Pausing time worked through Remote Events, so I tried searching online how to stop a remote event and nothing seemed to work. I don’t really know anything about Event:Disconnect() because I’m a terrible scripter.
Could someone please help me?!?!?!?!

If you don’t mind how is the pause time event fired?

RemoteEvent:

game.ReplicatedStorage.SpaceAbility.OnServerEvent:Connect(function(plr, player)
	player.PlayerGui.AbilityGUI.Enabled = true
	local COClone = game.ReplicatedStorage.CoolOrb
	local OMGClone = game.Workspace.OhMyGod:Clone()
	local PTClone = game.Workspace.TimeStop:Clone()
	OMGClone.Parent = workspace
	PTClone.Parent = workspace
	OMGClone.Name = workspace.OhMyGod.Name.."Clone"
	PTClone.Name = workspace.TimeStop.Name.."Clone"
	OMGClone:Play()
	wait(1.6)
	PTClone:Play()
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent.Name ~= player.Name and v.ClassName == "MeshPart" then
			v.Anchored = true
		end
	end
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent.Name ~= player.Name and v.ClassName == "Part" then
			v.Anchored = true
		end
	end
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "10"
	wait(1)
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "9"
	wait(1)
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "8"
	wait(1)
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "7"
	wait(1)
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "6"
	wait(1) 
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "5"
	wait(1) 
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "4"
	wait(1) 
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "3"
	wait(1) 
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "2"
	wait(1)
	player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = "1"
	wait(1) 
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent.Name ~= player.Name and v.ClassName == "MeshPart" then
			v.Anchored = false
		end
	end
	for i, v in pairs(game.Workspace:GetDescendants()) do
		if v.Parent:FindFirstChild("Humanoid") and v.Parent.Name ~= player.Name and v.ClassName == "Part" then
			v.Anchored = false
		end
	end
	PTClone:Play()
	PTClone.Ended:Wait()
	PTClone:Destroy()
	OMGClone:Destroy()
	local countdown = 30
	repeat wait(1)
		player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = ""..countdown..""
		print(countdown)
		countdown -= 1
	until
	countdown <= -1
	player.PlayerGui.AbilityGUI.Enabled = false
end)

May I ask why you have 2 player variables being passed through to the remoteEvent?

For some reason, I tried working with plr but it didn’t work. Player did the trick.

Im calling the remote event from 2 ways:
For mobile:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

if (UserInputService.KeyboardEnabled) then
	print("PC User")
else
	player.PlayerGui.EnableSpaceAbilityMobileGUI.Enabled = true
end

The Gui fires the remote event.

For non-Mobile Players:

local userInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if input.KeyCode == Enum.KeyCode.E then -- change the E to any key!
			game.ReplicatedStorage.SpaceAbility:FireServer(player)
			script.Disabled = true
			wait(35)
			player.PlayerGui.PressESpace.Disabled = false
		end 
	end
end)

I’ve revised your code a bit in the process.

What I’ve done is add a health check to the players humanoid. If it is equal to zero, the ability won’t work.

Feel free to give this a try.

local function timeStop(character, state)
	for _, v in pairs(workspace:GetDescendants()) do
		if v.Parent:FindFirstChildOfClass("Humanoid") ~= nil and not v:IsDescendantOf(character) then
			if v.ClassName == "MeshPart" or v.ClassName == "Part" or v:IsA("BasePart") then
				print(state)
				v.Anchored = state
			end
		end
	end
end


game.ReplicatedStorage.SpaceAbility.OnServerEvent:Connect(function(player)
	local character = player.Character
	if character == nil then return end
	local humanFound = character:FindFirstChildOfClass("Humanoid")
	if humanFound == nil then return end
	if humanFound.Health <= 0 then return end
	
	player.PlayerGui.AbilityGUI.Enabled = true
	
	local COClone = game.ReplicatedStorage.CoolOrb
	local OMGClone = game.Workspace.OhMyGod:Clone()
	local PTClone = game.Workspace.TimeStop:Clone()
	
	OMGClone.Name = workspace.OhMyGod.Name.."Clone"
	PTClone.Name = workspace.TimeStop.Name.."Clone"
	
	OMGClone.Parent = workspace
	PTClone.Parent = workspace
	
	OMGClone:Play()
	
	task.wait(1.6)
	
	PTClone:Play()
	
	timeStop(character, true)
	
	for looping = 10, 1, -1 do
		player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = tostring(looping)
		task.wait(1)
	end
	
	timeStop(character, false)
	
	PTClone:Play()
	
	PTClone.Ended:Wait()
	
	PTClone:Destroy()
	OMGClone:Destroy()
	
	local countdown = 30
	
	repeat
		task.wait(1)
		player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount.Text = ""..countdown..""
		print(countdown)
		countdown -= 1
	until countdown <= -1
	
	player.PlayerGui.AbilityGUI.Enabled = false
end)
1 Like

it doesnt work. and i know what your trying to do, but my game is pretty much like slap battles. when you die, you lose your tools and you get them back by going back to the arena.

Just check the humanoid’s health inside the callback function connected to the ‘RemoteEvent’ object.

local function OnRemoteFired(Player) --Remote receives player object argument.
	local Character = Player.Character --Get character from player.
	if not Character then return end
	local Humanoid = Character:FindFirstChildOfClass("Humanoid") --Get humanoid from character.
	if not Humanoid then return end
	if Humanoid.Health == 0 then return end --Check if humanoid's health is 0.
	--Do code.
end

hi @SinClosed

server script
local cooldown=10
remoteEvent=	game.ReplicatedStorage:FindFirstChild("RemoteEvent") or game.ReplicatedStorage:WaitForChild("RemoteEvent")

remoteEvent.OnServerEvent:Connect(function(plr)
	for i=cooldown,0,-1 do
		if i==0 then
			remoteEvent:FireClient(plr)
		end
		remoteEvent:FireClient(plr,"cooldown",i)	
		wait(1)
	end
	
end)

in local script add:

local script
local tool=script.Parent
tool.Activated:Connect(function()
	if debounce then return end
	debounce=true
	remoteEvent:FireServer()
end) --make sure you don't change the debounce here

remoteEvent.OnClientEvent:Connect(function(command,value)
	if command=="cooldown" then
                 local AbilityCount= player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount
		AbilityCount.Text=value
		debounce=true
	else
		debounce=false
	end	
end)

fire to server from client
server receives the event signal
server counts down till 0 and and controls debounce
in-between countdowns, server fires (plr, commandName, value)
something like this…

is there no way to pause or stop an remote event?

local script
local tool=script.Parent
tool.Activated:Connect(function()
	if debounce then return end
	debounce=true
	remoteEvent:FireServer()
end) --make sure you don't change the debounce here

remoteEvent.OnClientEvent:Connect(function(cooldown)
	for i=cooldown,0,-1 do
		local AbilityCount= player.PlayerGui.AbilityGUI.AbilityMain.AbilityCount
		AbilityCount.Text=value
		wait(1)
	end
	debounce=false
end)
server script
local cooldown=10
remoteEvent=	game.ReplicatedStorage:FindFirstChild("RemoteEvent") or game.ReplicatedStorage:WaitForChild("RemoteEvent")

local connection
function onServerEvent(plr)
	connection:Disconnect()
	remoteEvent:FireClient(plr,"cooldown")	
	wait(cooldown)
	connection=remoteEvent.OnServerEvent:Connect(onServerEvent)
end
connection=remoteEvent.OnServerEvent:Connect(onServerEvent)

something like this should work…
what it does is
create a variable called “connection”
assign remoteEvent.OnServerEvent:Connect(onServerEvent) as initial value
when the function is called
the function disconnect the event first [connection:Disconnect()] then continue with rest of the instruction provided
wait until cooldown fisnishes
then
assign a new value for the “connection” variable which is remoteEvent.OnServerEvent:Connect(onServerEvent): which repeats the above steps infinitely