Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my IntValue keep going above 100 even when i'm telling it not to?

repeat wait() until game.Players.LocalPlayer
local running = false

m = game.Players.LocalPlayer:GetMouse()

m.KeyDown:connect(function(key)
	if key == "0" and running == false then 
		print("Running")
		running = true
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 22 
		while running == true do
			game.StarterGui.run.Value.Value = game.StarterGui.run.Value.Value - 1
			local amount = game.StarterGui.run.Value.Value
			print (amount)
			wait(0.1)
		end
			
	end
end)

m.KeyUp:connect(function(key)
	if key == "0" and running == true then
		running = false
		local amount = game.StarterGui.run.Value.Value
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 11 
		while running == false and not amount == 100 or amount < 100 do
			local amount = game.StarterGui.run.Value.Value
			game.StarterGui.run.Value.Value = game.StarterGui.run.Value.Value + 1
			print(amount)
			wait(0.9)
		end
	end
end)

1 comment

0

I recommend using UserInputService instead of Player Mouse. https://developer.roblox.com/en-us/api-reference/class/UserInputService

2 answers

✓ Accepted answer

You should probably use math.clamp for stuff like this. math.clamp makes it not go below the minimum or go above the maximum.

game.StarterGui.run.Value.Value = math.clamp(game.StarterGui.run.Value.Value + 1, 0, 100)

The 0 is the minimum and the 100 is the maximum.

Your while conditions statement seems contradictory if its both checking that the value is not 100 anyways or under, remove the ‘not value == 100 or’, also you are doing an infinite loop each time you run a keyUp event function which will run it multiple times infinitely (likely why it goes up over 100), try to use a repeat loop. Also, you should be using UserInputService now instead of key events of mouse.

The other answer says you can use math.clamp which is an option as well if you want to use it.

This discussion is preserved as it was. New answers, comments, and votes are closed.