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

How do I keep a number above negative?

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

✓ Accepted answer

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

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