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

Why is my While statement not stopping when the test condition returns false?

Asked by 6 years ago

Currently, I am having troubles with a while statement not ending properly when a condition isn't met. In whole, I am trying to make it so when the key "q" is pressed down, it sets "Held" to true then starting this loop and when it is pressed up, it turns "Held" to false and then stops this loop. I have confirmed through prints during these functions that it's working correctly.

01while Held == true and wait() do
02    local debounce = false
03    if Held == false then
04        break
05    end
06    local ray = Ray.new(char.Head.Position, char.Head.CFrame.lookVector * FireRange)
07    local part, endPoint = workspace:FindPartOnRay(ray, char)
08    local beam = Instance.new("Part")
09    beam.FormFactor = "Custom"
10    --beam.Transparency = .4
11--  beam.Color = Color3.new(196, 40, 28)
12    --beam.Material = "Neon"
13    beam.Anchored = true
14    beam.Locked = true
15    beam.CanCollide = false
View all 28 lines...

Is this while statement not ending because the internals are somehow making it so it loops without checking the condition again? I have the whole loop inside a listener of the KeyDown event. If you are able to answer this, it would be appreciated if you left the issue and a solution.

0
wait() should NOT be a condition in while loops. And FormFactor is deprecated along with KeyDown.  User#19524 175 — 6y
0
How would I make it so the while statement would wait() after finishing it's first loop? I've never really used ray casting so I basically just copied and pasted that part from an older video. I don't know of any other way to detect if a Key has been pressed also. beeswithstingerss 41 — 6y
0
UserInputService. And add the wait() at the top or bottom of the loop, whichever is most convenient. User#19524 175 — 6y
0
Would the wait at the top of the loop be causing this issue? beeswithstingerss 41 — 6y
0
No but it is not recommended to use as a condition. It makes NO sense to use the return value of wait as a condition. User#19524 175 — 6y

Answer this question