Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

1

[INSERT BLUETASLEM COMMENT HERE]

1 answer

✓ Accepted answer
--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.

6 comments

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!

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#

0

It actually stops the scripts from working. soooo...

0

Semicolons don't stop the script from working. Please never say anything as completely bs as that ever again.

0

Test it.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.