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)
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