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 6 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?

01Underwater = script.Parent
02Underwater.Touched:Connect(function(hit)
03    if hit.Parent.ClassName == 'Model' and hit.Parent:FindFirstChild('Humanoid') and not hit.Parent:FindFirstChild('KillerTag') then
04        print("Head in water")
05        local character = hit.Parent
06        Air = character:FindFirstChild("Air")
07        local Humanoid = character:FindFirstChild("Humanoid")
08        if hit.Name == "Head" then
09            while Air.Value > -1 do
10                wait(1)
11                Air.Value = Air.Value - 1
12                while Air.Value <= 0 do
13                    Humanoid.Health = Humanoid.Health - 10
14                    wait(1)
15                end
View all 30 lines...

1 answer

Log in to vote
3
Answered by 6 years ago

You can use math.min and math.max

Ex:

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

Answer this question