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!
--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.