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

How do I keep a number above negative?

Asked by 5 years ago

Whenenver I'm in the part the air value will go above 100 and sometimes at -1 or -2. Also when the value is at 0 the player will not get hurt. What needs to be fixed?

Underwater = script.Parent
Underwater.Touched:Connect(function(hit)
    if hit.Parent.ClassName == 'Model' and hit.Parent:FindFirstChild('Humanoid') and not hit.Parent:FindFirstChild('KillerTag') then
        print("Head in water")
        local character = hit.Parent
        Air = character:FindFirstChild("Air")
        local Humanoid = character:FindFirstChild("Humanoid")
        if hit.Name == "Head" then
            while Air.Value > -1 do
                wait(1)
                Air.Value = Air.Value - 1
                while Air.Value <= 0 do
                    Humanoid.Health = Humanoid.Health - 10
                    wait(1)
                end
            end
        end
    end
end)
Underwater.TouchEnded:Connect(function(hit)
    if hit.Parent.ClassName == 'Model' and hit.Parent:FindFirstChild('Humanoid') and not hit.Parent:FindFirstChild('KillerTag') then
        local character = hit.Parent
        Air = character:FindFirstChild("Air")
        local Humanoid = character:FindFirstChild("Humanoid")
        while Air.Value < 101 do
            wait(1)
            Air.Value = Air.Value + 1
        end
    end
end)

1 answer

Log in to vote
3
Answered by 5 years ago

You can use math.min and math.max

Ex:


Air.Value = math.min(Air.Value + 1, 100) -- so it doesn't go > 100 Air.Value = math.max(Air.Value - 1, 0) -- so it doesn't go < 0
Ad

Answer this question