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
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.
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