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

Is it possible to check if a function/loop is running?

Asked by 8 years ago

Hey! I haven't asked a question in a while, but here is my issue. So say I want to run a loop when a key is pressed. This works, but I have a problem; I need to break the loop to be able to call another loop. I think I'm able to pull this off but I haven't figured out how. Here's the script I have:

--Wait for player--
repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
--Variables--
plr = game.Players.LocalPlayer
trial = game.Workspace.Trial
bike = trial.Bike
rider = trial.Rider
leans = bike.Leans
back = leans.Back
front = leans.Front
vert = leans.Vert

--Hold--
plr.Character.Humanoid.WalkSpeed = 0
plr.Character.Torso.Anchored = true

--Functions--
function LeanBack()
    while true do wait()
        rider.Pos.BodyPosition.Position = back.Position
    end
end

function LeanFront()
    while true do wait()
        rider.Pos.BodyPosition.Position = front.Position
    end
end

--Key downs--

--The important part--
local Mouse = plr:GetMouse()
Mouse.KeyDown:connect(function(key)
    if key == "a" then
    --here I was thinking an if statement such as [if LeanFront.IsPlaying then LeanFront break end] or something. I really can't figure out how.
        LeanBack()
    end

    if key == "d" then
    --same here
        LeanFront()
    end
end)

Thanks! If you could link me to any sources on something like this, that would be great!

1
[INSERT BLUETASLEM COMMENT HERE] UserOnly20Characters 890 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago
--Wait for player--
repeat wait() until game.Players.LocalPlayer
repeat wait() until game.Players.LocalPlayer.Character
--Variables--
plr = game.Players.LocalPlayer
trial = game.Workspace.Trial
bike = trial.Bike
rider = trial.Rider
leans = bike.Leans
back = leans.Back
front = leans.Front
vert = leans.Vert
local a;

--Hold--
plr.Character.Humanoid.WalkSpeed = 0
plr.Character.Torso.Anchored = true

--Key downs--

--The important part--
local Mouse = plr:GetMouse()
Mouse.KeyDown:connect(function(key)
    if key == "a" then
    --here I was thinking an if statement such as [if LeanFront.IsPlaying then LeanFront break end] or something. I really can't figure out how.
       a=true;
    end

    if key == "d" then
a=false;
    end
end)
Mouse.KeyUp:connect(function(k)
a = nil;
end);

while wait() do
if a then
rider.Pos.BodyPosition.Position = back.Position
elseif a == false then
rider.Pos.BodyPosition.Position = front.Position
else
-- If they've just let go you should probably do something
end
end

That should work. It's bad, but I can't be bothered to make it better without shamelessly suggesting use of my modules.

0
First I'd like to say, indenting would really help, plus this isn't JavaScript, so the semi colons are unnecessary. But I fixed it up and it works! So thanks! minikitkat 687 — 8y
0
Are you being picky about my style? I really don't care if it's not JS, I use semicolons 'cause I'm from C# User#6546 35 — 8y
0
It actually stops the scripts from working. soooo... minikitkat 687 — 8y
0
Semicolons don't stop the script from working. Please never say anything as completely bs as that ever again. User#6546 35 — 8y
View all comments (2 more)
0
Test it. minikitkat 687 — 8y
0
Don't need to; I read it, I write like this. If you think my semicolons are breaking code, kindly explain how I've never noticed it. User#6546 35 — 8y
Ad

Answer this question