How to limit a numbervalue?

I am trying to limit my leaderstats to the value of rebirths with a minimum value of 1 and a maximum of 1 billion
I had tried to limit using the IntConstrained value, but it only limited to the number 10

3 Likes

I think you could use math.clamp.

1 Like

You can make it so when the Rebirths has to be updated, you just clamp it between the amount you want

Example

Rebirths.Value = math.clamp(Rebirths.Value + 1, 1, 1000000000)

If the number is lower than 1 or higher than that number, it’ll just clamp it to the lowest or higher number it can be respectively.

Or you can just use a changed event to update the Rebirths value when it reaches a certain amount

Rebirths.Changed:Connect(function(newVal)
	Rebirths.Value = math.clamp(newVal, 1, 10000000)
end)
3 Likes

You could use math.clamp.
Like this:
Let’s put the script in stats (doesn’t really matter)

local rebirth = script.Parent.Rebirths
val = rebirth.Value
rebirth.Value = math.clamp(val + 1, 0, 1000000000)
--rebirth.Value = math.clamp(val + actualnumber, minimum, maximum)
1 Like

ok i tried to do what you said but it seems that the value is not limiting

I used the value 2000 to be able to test

  local rebirth = script.Parent.Rebirths
     rebirth.Changed:Connect(function(value)
         rebirth.Value = math.clamp(value,0,2000)
end)
1 Like

I had used this code about 10 minutes ago and it didn’t work :frowning:

Hmmm that is really strange…

Is the event actually being fired?

I just found this in the output
Screenshot_31

1 Like

it counts as if there were no rebirths in the stats
Screenshot_32

1 Like
local rebirth = script.Parent--.Rebirths
     rebirth.Changed:Connect(function(value)
         rebirth.Value = math.clamp(value,0,2000)
end)

Fixed code. You could also put the script into stats.

1 Like

it seems that the error no longer appears in the output, but is still not limiting the rebirth value

The reason for this is because of references.

value refers to Rebirth.Value, which is a reference to the number, not the property. Therefore, when you try to change the value, instead of pointing to the number in Rebirth.Value, it instead just has a normal number in it, like 20. So when you use value = 20, you’re just changing what’s saved in the value variable, not changing the property.

Instead, do rebirth.Value = math.clamp(rebirth.Value,0,2000)

I tried to do what you said but it seems that there were no results

it was for me to launch this today in the update of my game but it seems that I will have to postpone :frowning:

I tested this in a very simple script, inside a ClickDetector, and it worked.

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = player:FindFirstChild("leaderstats") or Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	local Rebirths = leaderstats:FindFirstChild("Rebirths") or Instance.new("IntValue", leaderstats)
	Rebirths.Name = "Rebirths"
	Rebirths.Changed:Connect(function()
		Rebirths.Value = math.clamp(Rebirths.Value, 0, 2000)
	end)
end)

script.Parent.MouseClick:Connect(function(player)
	player.leaderstats.Rebirths.Value += 250
end)

Make sure you’re utilizing your Client Data and Server Data properly. Changes to the client might not show up on the server, and the server might instead over-ride your client data.

Also, it’s possible your UI is displaying incorrect information through the client. Check the actual value on the server.

1 Like

is it possible that the leaderstats script is affecting the rebirth limit?

local PlayerStatsDS = game:GetService("DataStoreService"):GetDataStore("Player_StatsOL2")

game.Players.PlayerAdded:Connect(function(NP)
	
	local Key = "PDS-".. NP.UserId
	
	local GetSave = PlayerStatsDS:GetAsync(Key)
	
	local PSF = Instance.new("Folder", NP)
	PSF.Name = "leaderstats"
	
	local StatsFolder = script.Stats
	
	for _, S in pairs(StatsFolder:GetChildren()) do
		local NS = Instance.new(S.ClassName, PSF)
		NS.Name = S.Name
		NS.Value = S.Value
	end
	
	local timeplayed = PSF:WaitForChild("Time Played")
	
	spawn(function()
		while true do
			wait(1)
			timeplayed.Value = timeplayed.Value + 1
		end
	end)
	
	if GetSave then
		for n, Stat in pairs(PSF:GetChildren()) do
			Stat.Value = GetSave[n]
		end
	else
		local STS = {}
		for _, Stat in pairs(StatsFolder:GetChildren()) do
			table.insert(STS, Stat.Value)
		end
		PlayerStatsDS:SetAsync(Key, STS)
	end
end)

game.Players.PlayerRemoving:connect(function(OP)
	
	local Key = "PDS-".. OP.UserId
	
	local StatsFolder = OP.leaderstats
	
	local STS = {}
	for _, Stat in pairs(StatsFolder:GetChildren()) do
		table.insert(STS, Stat.Value)
	end
	PlayerStatsDS:SetAsync(Key, STS)
end)

At a quick glance, I’m not seeing anything that would impact your Rebirth Value in such a way that it would bypass the clamp.