I think I may just misunderstand how coroutines work, especially in terms of resuming and yielding.
I have a piece of code which runs from a remote function. The coroutine in question acts to stop player movement. The issue is that following the code within the remote function, I want the coroutine to stop and the player’s movement to be allowed. At the moment the coroutine continues running (or at least the function encapsulated continues running) even when I call for it to be yielded. The weirdest part is that when I print coroutine.status following resuming the coroutine it prints suspended.
local function MovementFunc (player)
while wait() do
player.Character.Humanoid.WalkSpeed = 0
print(player.Character.Humanoid.WalkSpeed)
end
end
local MovementStop = coroutine.create(MovementFunc)
RemoteFunction.OnServerInvoke =
function(player, mouseTarget)
coroutine.resume(MovementStop, player)
print(coroutine.status(MovementStop))
--prints suspended
wait()
coroutine.yield(MovementStop)
print(coroutine.status(MovementStop))
-- doesn't print anything
end
If anyone has a better solution for stopping player movement from the server that would also be good.
I think you’re overthinking it. WalkSpeed doesn’t need to be set on a loop, you can just set it once.
1 Like
Oh true. I completely missed that. The coroutine is still puzzling though.
edit: Exploiters can change walk speed on the client though right? The only reason I thought about doing a while loop was to stop that from occurring.
I’m not the guy to answer it. From my very limited experience, they don’t seem to function like you’d expect they would. I gave up on coroutine control ages ago. When I use them, it’s just create and resume.
1 Like
You yielded the thread, thus why the print
isn’t outputting the coroutine status. You need to remove coroutine.yield
for it to continue.
That doesn’t explain anything. coroutine.status should return a value whether the coroutine is running or not. Even if that was the case it doesn’t explain why it prints suspended directly after I resume the coroutine, or why it runs regardless of being supposedly suspended.
local StopMovement = coroutine.create(function()
while wait() do
--stuff here
end)
RemoteFunction.OnServerInvoke =
function(player, mouseTarget)
coroutine.resume(StopMovement, player)
print(coroutine.status(StopMovement))
wait()
coroutine.yield(StopMovement)
end
--Maybe this will do
also theres this:
local success, result = coroutine.resume(StopMovement)
print(success, result) if success is true, it probably resumed
Declaring the function outside of the coroutine is the same as declaring it inside, so it has the same result.
The success, result printed true and nil since the coroutine successfully runs and doesn’t return anything.
Yeah, I kinda knew that. But if the success is true, why doesn’t it print yielded?
You can’t do anything about them changing their walk speed. Basically network ownership allows the client to have complete control over an object. Now the client has network ownership over the player (for smoother gameplay and less delay). Now since they can change it pretty sure this woukdn’t work.
I know the player is controlled by the client. I don’t really see why it wouldn’t work. But even then, at the moment I’m more interested in why the coroutine isn’t running properly.
Fair enough I’ll try read the code and work it out
Also can you put print statements inside of it should help to see where it runs and doesn’t.
Also you can use coroutine.wrap to use it as if it’s spawn if that helps.
The print inside the while loop prints ‘0’ every wait() interval, suggesting it’s running. The first coroutine.status prints suspended, the second coroutine.status (directly after the coroutine.yield) doesn’t print at all. If I remove the coroutine.yield the second also prints suspended, which makes no sense whatsoever.