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

How could I use math.clamp inside of a leaderstats script?

Hey, I was wondering, how could I use math.clamp inside of a leaderstats script here’s what I had an idea of using

game.Players.PlayerAdded:Connect(function(player)
local leader = Instance.new(“Folder”)
leader.Name = “leaderstats”
leader.Parent = player

local coins = Instance.new(“NumberValue”)
coins.Name = “Coins”
coins.Parent = leader

local space = Instance.new(“NumberValue”)
space.Name = “Space”
space.Parent = player

coins.Changed:Connect(function()
local max = space
if coins > max then
coins = math.clamp(coins.Value, 0, space.Value)

Would this script work if I tried it?

1 answer

✓ Accepted answer

kinda, you're using the function properly, but you're assigning the coins value object to a number value which the game doesn't understand so you instead need to assign coins.Value = math.clamp(coins.Value, 0, space.Value)

also, i don't know understand why you don't want to assign it the max value if it goes over the max value.

in line 15, assign max to space.Value to make max a number value instead of a reference to the space value object. the if statement should also be if(coins.Value > max)

other than that, your code shouldn't run with any errors and you're using the clamp function properly.

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