Anyone know how to do this with while loops?

I want to create something where in a local script theres a while loop and as time goes the players speed increases at a rate, but when the players walkspeed reaches a certian number that increasing part stops and instead decreases then when the walkspeed decreases to a certain number it starts increasing again and the decreasing stops. i tried to do this but instead it just keeps increasing, anyone know how to do it?

This was my attempt at it

while true do
	wait ()
	if script.Parent.Humanoid.WalkSpeed >= 95 then
		script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + 0.009
	elseif script.Parent.Humanoid.WalkSpeed <= 95 then 
		script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + 0.025
	end
end

Could do something like this:

local Humanoid = script.Parent.Humanoid

local Rate = 0.009
while true do
	task.wait(1/30)
	local Current = Humanoid.WalkSpeed
	
	if Current >= 95 then
		Rate = -0.009
	elseif Current <= 50 then
		Rate = 0.025
	end
	
	Humanoid.WalkSpeed += Rate
end

You put a + when you should’ve put a - on line 3
Also on line 4 the operator should just be a < instead of a <=

while true do
	if script.Parent.Humanoid.WalkSpeed >= 95 then
		script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed - 0.009
	elseif script.Parent.Humanoid.WalkSpeed < 95 then 
		script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed + 0.025
	end
	task.wait()
end

Hope this helps!

1 Like
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild("Humanoid")

local minSpeed, maxSpeed, rate = 10, 50, 0.5
local direction = 1 

while task.wait(0.1) do
	humanoid.WalkSpeed += rate * direction
	if humanoid.WalkSpeed >= maxSpeed then
		direction = -1
	elseif humanoid.WalkSpeed <= minSpeed then
		direction = 1
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.