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.

while Held == true and wait() do
    local debounce = false
    if Held == false then
        break
    end
    local ray = Ray.new(char.Head.Position, char.Head.CFrame.lookVector * FireRange)
    local part, endPoint = workspace:FindPartOnRay(ray, char)
    local beam = Instance.new("Part")
    beam.FormFactor = "Custom"
    --beam.Transparency = .4
--  beam.Color = Color3.new(196, 40, 28)
    --beam.Material = "Neon"
    beam.Anchored = true
    beam.Locked = true
    beam.CanCollide = false
    local distance = (char.Head.CFrame.p - endPoint).magnitude
    beam.Size = Vector3.new(0.05, 0.05, distance)
    beam.CFrame = CFrame.new(char.Head.CFrame.p, endPoint) * CFrame.new(0, 0, -distance / 2)
    beam.Parent = workspace
    game:GetService("Debris"):AddItem(beam, (1/290))
    if part ~= nil then
        local human = part.Parent:FindFirstChild("Humanoid")
        if human and (not debounce) and (human.Parent ~= char) then
                debounce = true;
                char.Humanoid.Health = char.Humanoid.Health - .066
        end
    end         
end

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