Measuring a Player's Walkspeed and Incrementing, ModuleScript Logic

	while i < 8 do
		if humanoid.WalkSpeed >= 80 then
			while humanoid.WalkSpeed >= 80 do
				print("checked")
				task.wait(0.05)
			end
		elseif humanoid.WalkSpeed < 80 then
			i = i + 1
			print("counting.. ")
			print(i)
			task.wait(1)
		end
	end

This function I have created runs in a ModuleScript. Its intent is to measure a player’s walkspeed and pass “checked” if it finds it to be over 80.

As of now, I have created a boost pad that allows the player to temporarily increase their speed. The general code it uses is unimportant, but of note is that it incrementally lowers the Player’s walkspeed after setting it to a high value.

As the player’s walkspeed decreases from 82 to 78, the function counts one more time (“counting… i”) before halting. The intent is for the function to count up to 8.

Is there any reason the ModuleScript ‘skips over’ the if statement?

print also the speed and check in one go
print("counting..", i, "speed=", humanoid.WalkSpeed)
off by one error usually is your own logic error. if statement is usually not the problem, but you can simplify your checking

while i < 8 do
  if humanoid.WalkSpeed >= 80 then 
  ...
  else
  ...
end

Thank you for the tip. I have simplified my code.
Here is how the boost pad works and the accompanying Walkspeed values if this is of any assistance.

Also attached is the values when using both the boost pad and the counter.
values

			print("counting.. ")
			print(i)
			print(humanoid.WalkSpeed)
			task.wait(1)

not sure how it would work
because the code is not controlling the humanoid speed,
and it doesn’t accomplish anything other than just ‘counting’

you should do a full hand trace yourself like the following:
assume i starts with 0, current speed 82

while `0` < 8 <<< yes  
  if speed >= 80 then <<< yes
     while speed >= 80 do <<< judging from your video, you speed -=2 after some time,
     end
     <<< we will exit the loop when it is at 78
  else
    ...
  end
end

then we are at speed 78

while `0` < 8 <<< yes
  if speed >= 80 then <<< no
    ...
  else
    i += 1
    print("counting.. ", `1`)
    task.wait(1)
end

i don’t know how and when the speed would decrease further,
but because i < 8 then, it will keep looping if the speed is below 80, and i is not 8 yet

1 Like

Hello,
Thank you for your assistance. However, I have decided that since modifying this logic to fit the correct bounds would take me another half hour,

I am tired.

I have decided to disband this thread. I will be utilizing another solution.
Thank you for the assistance nonetheless.

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