I have my script set up where a loop is resumed when the player is walking and is paused while the player is stopped. When I run the script it runs correctly for the first time, but after that the loop seems to not work. Does anyone know what is wrong with my script?
local Camera = workspace.CurrentCamera local Player = game.Players.LocalPlayer local Figure = Player.Character local Mouse = Player:GetMouse() local i = 0 local W = false CamMovment = coroutine.create(function() while W == true do wait() print(i) i = i + 1 end end) Mouse.KeyDown:connect(function(key) if key == "w" then W = true coroutine.resume(CamMovment) end end) Mouse.KeyUp:connect(function(key) if key == "w" then W = false coroutine.yield(CamMovment) end end)
The coroutine is a separate thread. The while loop is in that thread. When W is set to false, the while loop stops. Resuming the thread does not reboot it. Here's something better you can do:
local Camera = workspace.CurrentCamera local Player = game.Players.LocalPlayer local Figure = Player.Character local Mouse = Player:GetMouse() local i = 0 local W = false Mouse.KeyDown:connect(function(key) if key == "w" then W = true while W do wait() print(i) i=i+1 end end end) Mouse.KeyUp:connect(function(key) if key == "w" then W = false end end)
You don't need a coroutine.