Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
royaltoe 5144 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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.

Ad

Answer this question