How to interrupt a remote event?

Hello

Currently in the project I’m working on, I have a remote event that triggers a sequence of events for a player. How would I stop the current event and make it restart if it was called by the player again? Normally I’d use something like

Action = 0
function Stuff ()
 Action = Action +1
 local NewAction = Action
 wait(1)
 if Action == NewAction then
   --stuff only happens now if the function wasn't called again
 end 
end

but I am unsure how I could utilize something like this since I’m using a Remote Event that can be called by multiple players.

--this is what im working on for reference 
game.ReplicatedStorage.FlickPlayer.OnServerEvent:connect(function(player, Character, Direction, Power, Lift)---Player, Character, Direction, Power, Lift

	if Character~=nil and Character:FindFirstChild("HumanoidRootPart")~=nil then
		local body = Character:FindFirstChild("HumanoidRootPart")
		if body and body:FindFirstChild("FlickHand")~=nil then 
			
		
			Character.States.Flung.Value = true
			Character.States.Attack.Value = true
			Character.Humanoid.PlatformStand= true
			Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			game:GetService("RunService").Heartbeat:Wait()--Velocity is logged to local players otherwise?
			body.Velocity = Direction*Power+Vector3.new(0,Lift,0)
		 	
            wait(Power/60) -- How would I make this function stop here if the remote event was called again?
			
			Character.States.Attack.Value = false
			body.FlickHand.Enabled = false
			Character.Humanoid.PlatformStand= false	
			body.CFrame = CFrame.new(body.Position.X,body.Position.Y,body.Position.Z) 
			body.Velocity = Vector3.new()
			body.RotVelocity = Vector3.new()
			print("Standing up")]]
		end
	end
end)

I’m sorry if my question isn’t making since, I am very bad with terminology :frowning:

you can put a :Connect() into a variable and disconnect, like so:

local remote = -- your remote event

local connection

connection = remote.OnServerEvent:Connect(function()

end)


connection:Disconnect()

Is this the best way to go about it? The remote event is sometimes called by different local scripts i have set up

id recommend a coroutine-based approach, where you use coroutines to yield the event

pseudo-code example

routines -> {}

@remote.server()
fn flickPlayer(player, ...)
	fn createRoutine(...)
		local routine = coroutine.create(...) -- your function here
		coroutine.resume(routine)
		return routine

	if not routines[player]
		routines[player] -> createRoutine(fn)
	else
		coroutine.yield(routines[player])
		routines[player] -> createRoutine(fn)

If you handle the receiving server side all in the same script, it’ll work the same, as soon as you use :Disconnect(), the functions running on it will stop

Oh okay. I’m not sure if im reading this correctly, but the routines{} table at the top would basically be taking the role of my Action, NewAction type of thing, right? If so, for the table, would I long term need to manage the table? Remove players from it when they leave the game etc?

no, because the coroutines should be managed already, deleting a thread whenever a new one is created, keep in mind im not the most experienced in using coroutines in this type of use case, so take it with a grain of salt

but if anyone has any outside objections for a use-case like this, that’d be great.

functions end immediately once return is calld
code after return will error
at least to my knowledge

it’s a function in a function, it will not error because it returns in the function that creates the coroutine

I’m sorry for being so dense, but I’m having a hard time understanding the pseudo code. could you help me get towards the final step here?

routines = {}
game.ReplicatedStorage.FlickPlayer.OnServerEvent:connect(function(player, Character, Direction, Power, Lift)---Player, Character, Direction, Power, Lift
	function CreateRoutine()
		local routine = coroutine.create(flick) -- your function here
		coroutine.resume(routine)
		return routine
		
	end
	if not routines[player] then
		routines[player] = createRoutine(function())
		else
		coroutine.yield(routines[player])
		routines[player]
	end
end)

Code after a return statement will not error it just won’t be executed, this is a commonly used to practice to prevent a function from executing any further.

Yes you would need to manage the table, if you’re inserting data into a table when a player joins then subsequently you’ll need to remove that same data when the player leaves.

yea i clearly wrote the pseudocode in a way that isn’t very understable unless you know python/rust, that’s my fault

anyway here’s a lua based example

local routines = {}

local function onServerEvent(player, Character, Direction, Power, Lift)
	local function createRoutine()
		local routine = coroutine.create(function()
			if Character ~= nil and Character:FindFirstChild("HumanoidRootPart") ~= nil then
		local body = Character:FindFirstChild("HumanoidRootPart")
		if body and body:FindFirstChild("FlickHand")~=nil then 
			
		
			Character.States.Flung.Value = true
			Character.States.Attack.Value = true
			Character.Humanoid.PlatformStand= true
			Character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
			game:GetService("RunService").Heartbeat:Wait()--Velocity is logged to local players otherwise?
			body.Velocity = Direction*Power+Vector3.new(0,Lift,0)
		 	
            wait(Power/60) -- How would I make this function stop here if the remote event was called again?
			
			Character.States.Attack.Value = false
			body.FlickHand.Enabled = false
			Character.Humanoid.PlatformStand= false	
			body.CFrame = CFrame.new(body.Position.X,body.Position.Y,body.Position.Z) 
			body.Velocity = Vector3.new()
			body.RotVelocity = Vector3.new()
			print("Standing up")
			end
		end)

		coroutine.resume(routine)
		return routine
	end

	if routines[player] then
		coroutine.yield(routines[player])
	end

	routines[player] = createRoutine()
end