Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
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)

0
I recommend using UserInputService instead of Player Mouse. https://developer.roblox.com/en-us/api-reference/class/UserInputService killerbrenden 1537 — 4y

2 answers

Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years ago

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.
Ad
Log in to vote
0
Answered by
aredanks 117
4 years ago
Edited 4 years ago

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.

Answer this question