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

How would one go about making stats not go negative?

3 answers

(Assuming you converted your stringvalue to an intvalue,)

You could use

intValue.Changed:Connect(function(val)
	intValue.Value = math.abs(val) == val and val or 0
end)

However, you should always check if it is negative before assigning a value to the stat.

Hope this helps!

First off, I believe a StringValue can NOT go negative unless you do

local Number = tonumber('-1')

But:

If it was a intvalue you can used .Changed event to detect change and if it goes below 0 then change it back to 0:

local IntValue = Instance.new('IntValue')
IntValue.Value = 0

IntValue.Changed:Connect(function()
	if IntValue.Value < 0 then
		IntValue.Value = 0
	end
end)

This will prevent the value going below 0. Hopefully, this helped. Good luck!

Whenever you update the stat, you can use math.clamp to clamp the value between a minimum and a maximum. Example:

local currentStat = 0
local intvalue = workspace.IntValue
function updateStat(stat)
	local newStat = math.clamp(stat, 0, 9999999999)
	intvalue.Value = newStat
end

while true do
	wait(1)
	updateStat(currentStat + 1)
end

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