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.