Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with my script?

Asked by
xApRix 25
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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?

01local Camera = workspace.CurrentCamera
02local Player = game.Players.LocalPlayer
03local Figure = Player.Character
04local Mouse = Player:GetMouse()
05 
06local i = 0
07local W = false
08 
09CamMovment = coroutine.create(function()
10    while W == true do
11        wait()
12        print(i)
13        i = i + 1
14    end
15end)
View all 29 lines...

1 answer

Log in to vote
2
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

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:

01local Camera = workspace.CurrentCamera
02local Player = game.Players.LocalPlayer
03local Figure = Player.Character
04local Mouse = Player:GetMouse()
05 
06local i = 0
07local W = false
08 
09Mouse.KeyDown:connect(function(key)
10    if key == "w" then
11        W = true
12        while W do
13            wait()
14            print(i)
15            i=i+1
View all 24 lines...

You don't need a coroutine.

Ad

Answer this question