Max value doesnt work

Hi , so i made a simulator game and i added max value

but when i add the max value

it’s stuck at 10
this is the script

local leaderstats = Instance.new("Folder", pl)
leaderstats.Name = "leaderstats"

local Max = game.ReplicatedStorage.MaxValue.MaxValue

local food = Instance.new("IntValue", leaderstats)
food.Name = "Foods"
food.Value = 0

food.Changed:Connect(function(value)
	if value > Max.Value  then
		food.Value = Max.Value
	end
end)

local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money"
Money.Value = 0

end)

and the add max value script

local pl = game.Players.LocalPlayer
local leaderstats = pl.leaderstats
local Money = leaderstats.Money
local MaxValue = game.ReplicatedStorage.MaxValue.MaxValue
local price = 50

script.Parent.MouseButton1Click:Connect(function()
if Money.Value >= price then
Money.Value = Money.Value - price
MaxValue.Value = 30
end
end)

3 Likes

Hi, just a few things that I would like to check:

What is this value equal to?

What fires this event? I can’t see anything in the scripts that would cause this value to change.

Thanks!

1 Like

i dont know the code is from
How can I make a max Value leaderstat?

and i change the max value to my own value

When the error occurred, what did you have the max value set to?

1 Like

i set the max value to 10 and theres no error from the output

and when i added the max value its keep stuck in 10

It looks to me as though the food value is somehow greater than 10 originally, and is being clamped whenever it tries to change. Try adding this at the end of the first script:

while true do
print(food)
wait()
end

If it is constantly printing a number greater than 10, then you will know that this is the issue.

game.Players.PlayerAdded:Connect(function(pl)
local leaderstats = Instance.new(“Folder”, pl)
leaderstats.Name = “leaderstats”

local Max = game.ReplicatedStorage.MaxValues.MaxValue

local food = Instance.new("IntValue", leaderstats)
food.Name = "Foods"
food.Value = 0

food.Changed:Connect(function(value)
	if value > Max.Value  then
		food.Value = Max.Value
	end
end)

local Money = Instance.new("IntValue", leaderstats)
Money.Name = "Money"
Money.Value = 0

while true do
	print(food)
	wait()
end

end)

like this?

Just use math.clamp(), math.max(), or math.min() instead, they are intended for this purpose

how i use that im newbie hehehe

have you tried this?

food.Value = math.clamp(food.Value, 0, Max.Value)

not working im trying aaaaaaaaa

Just to clarify what Dexter is meaning, this is how you should set it up. Replace this code:

food.Changed:Connect(function(value)
	if value > Max.Value  then
		food.Value = Max.Value
	end
end)

With this code:

food.Changed:Connect(function(value)
food.Value = math.clamp(value, 0, 10)
end)

its keep stuck at 10

i want to like upgrade the max value

and this is the food value giver script

game.ReplicatedStorage.AddPoint.OnServerEvent:Connect(function(Player, Value)
Player.leaderstats.Foods.Value += Value
end)

Set it up like this and take a screenshot of what the output says

theres no error

just infinite yeld for my shop