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