Min/Max Values Module

Morning, I’m just messing around with a leaderstats script, and I was wondering how I could link the min/max values to an “IntValue” I have created the values property changer and placed it in replicated storage.

Thanks. :slight_smile:

local player = game:GetService("Players")

function spawnedplayer(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	--Currency System
	local Min = 0 -- Changable values
	local Max = 5000 -- Changabele values
	
	local Currency = Instance.new("IntValue")
	Currency.Name = "Coins"
	Currency.Value = Min
	Currency.Parent = leaderstats
	
	Currency.Changed:Connect(function()
		if Currency.Value <= Min then
			Currency.Value = Min
		elseif Currency.Value >= Max then
			Currency.Value = Max
	
		end
	end)
end

player.PlayerAdded:Connect(spawnedplayer)

STEP 1
Go to CurrencyModifier in ReplicatedStorage

STEP 2
Create two new Attributes

  1. image

  2. image

  3. image

STEP 3

Go to your script and paste this

local player = game:GetService("Players")

function spawnedplayer(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	--Currency System
	local Min = game.ReplicatedStorage..CurrencyModifier.MinValue -- Changable values
	local Max = game.ReplicatedStorage..CurrencyModifier.MaxValue -- Changabele values

	local Currency = Instance.new("IntValue")
	Currency.Name = "Coins"
	Currency.Value = Min
	Currency.Parent = leaderstats

	Currency.Changed:Connect(function()
		if Currency.Value <= Min then
			Currency.Value = Min
		elseif Currency.Value >= Max then
			Currency.Value = Max

		end
	end)
end

player.PlayerAdded:Connect(function(Player)
	spawnedplayer(Player)
end)
1 Like

math.clamp exists

Currency.Changed:Connect(function()
	Currency.Value = math.clamp(Currency.Value, Min, Max)
end)

Do you wanna link the values from the IntValue or to the IntValue from the script?

In case i misread your post. This part

When you create the Currency Intvalue, call :SetAttribute(“Min”, 0) and :SetAttribute(“Max”, 5000) on it and use that to clamp it’s values

1 Like

Yeah, I also misread twice, so I think you’re right.

I basically want to limit a currency

If I’m correct in understanding, this wouldn’t be needed if I use IntValue?

	Currency.Changed:Connect(function()
		if Currency.Value <= Min then
			Currency.Value = Min
		elseif Currency.Value >= Max then
			Currency.Value = Max

Do I leave the value blank here…

SH2

Yeah, you do leave it blank there

1 Like

Let me know if the method I did show you did work! :smiley:

Getting a good fair few issues going on here.

EDITED:

I’d like to thank @LightingLion58 for helping me with the solution, using math.clamp as the right and additional method.

local player = game:GetService("Players")

local MinV = game.ReplicatedStorage.CurrencyModifier:GetAttribute("MinValue")
local MaxV = game.ReplicatedStorage.CurrencyModifier:GetAttribute("MaxValue")

function spawnedplayer(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	--Currency System
	local Min = MinV -- Changable values
	local Max = MaxV -- Changabele values

	local Currency = Instance.new("IntValue")
	Currency.Name = "Coins"
	Currency.Value = Min
	Currency.Parent = leaderstats
	
	Currency.Value = math.clamp(Currency.Value, Min, Max)
	
	Currency.Changed:Connect(function()
		Currency.Value = math.clamp(Currency.Value, Min, Max)
	end)
end

player.PlayerAdded:Connect(function(Player)
	spawnedplayer(Player)
end)

image
image
There you can see that it should be working right now, try pasting the newer script.

Do I need the if statement with this method?

Yeah, keep that else it won’t work

Thank you. Still learning scripting, My only question now is, why bother doing that if my if statement is enough?

I found an issue tho where it changes to the max thing, give me a few minutes to fix it.

local player = game:GetService("Players")

local MinV = game.ReplicatedStorage.CurrencyModifier:GetAttribute("MinValue")
local MaxV = game.ReplicatedStorage.CurrencyModifier:GetAttribute("MaxValue")

function spawnedplayer(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	--Currency System
	local Min = MinV -- Changable values
	local Max = MaxV -- Changabele values

	local Currency = Instance.new("IntValue")
	Currency.Name = "Coins"
	Currency.Value = Min
	Currency.Parent = leaderstats
	
	if Currency.Value <= Min then
		Currency.Value = Min
	end

	if Currency.Value >= Max then
		Currency.Value = Max
	end
	
	Currency.Changed:Connect(function()
		if Currency.Value <= Min then
			Currency.Value = Min
		end
		
		if Currency.Value >= Max then
			Currency.Value = Max
		end
	end)
end

player.PlayerAdded:Connect(function(Player)
	spawnedplayer(Player)
end)

This will check if the player is over the max limit when joining the game also

Can also be written as:

Currency.Changed:Connect(function()
	Currency.Value = math.clamp(Currency.Value, Min, Max)
end)

math.clamp returns value if it is between min and max. If bigger, returns max, else returns min.

Use @DevCafe_Protector 's solution as it is best.

But as a fun fact, I’d also like to mention that there used to be a class called IntConstrainedValue
It is now deprecated in favor of math.clamp and shouldn’t be used, but it does exactly this (clamps an IntValuebetween MinValue and MaxValue).

3 Likes