How to prevent Sell Value to go up a certain limit

Hello guys, I’m currently developing a Simulator and I need help with this.
When the player sells his Muscles, they get converted in Coins, but the Muscles should have a limit given by the Storage the player owns. So the issue is: If I have a weight that gives me 16 muscles for each lift, and my Storage Limit is 30, after 2 times that I lift the weight, instead of having 32 muscles, that 32 should become 30. This actually happens, but the issue is that yes, the value of the muscles becomes the exact value of the Storage, but the excess muscles remain stored somewhere and still get sold like if you had more points, past the limit of the Storage.

So, instead of selling for 30 Coins, I sell 30 Muscles but I get 32.
I was thinking about creating a RemoteEvent that could tell the server that the player was going past the given limit, but I don’t think it is a good idea, and if it is, any clue on how to organise that?
Thank you!

1 Like

Use math.clamp

math.clamp(value, min, max)
1 Like

This is fairly simple, make use of math.clamp

local muscle = math.clamp(muscle, 0, 30)

math.clamp is used like so:

local value = math.clamp(value, minimumValue, maximumValue)

The value is unable to be lower than the minimumValue and unable to be greater than the maximumValue.
Here’s the API Document if you’re still unsure: math | Documentation - Roblox Creator Hub.

1 Like

You could use math.clamp to acheive this:

math.clamp(value, minimum, maximum)
1 Like

Yeah, I knew about math.clamp but never thought of using it, it was so simple tho, thank you very much to everybody!

2 Likes

Just another question, I just got error, the math.clamp requires a Number so I can’t put “muscle” since it is an Instance, any idea on how to trick this?

This is the Sell script

local deb = false 
local part = script.Parent
part.Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
	if Humanoid then
		local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
		if player then
			if deb == false then
				deb = true
				script.Parent.Sell:Play()
				local leaderstats = player:WaitForChild("leaderstats")
			local OtherStats = player:WaitForChild("OtherStats")
			local money = OtherStats.Coins
			local Storage = OtherStats:WaitForChild("Storage").Value -- Default is 30 
			local Muscles = leaderstats:FindFirstChild("Muscle")
				local Stage = leaderstats:FindFirstChild("Stage")
				Muscles.Value = math.clamp(Muscles, 0, 30)  -- At first I tried (Muscles, 0, Storage) but the same error occured
				if Muscles.Value >= 0 then
					money.Value = Muscles.Value + money.Value
					Muscles.Value = 0
					
				end
					
						wait(2)
						deb = false
					script.Parent.Sell:Stop()
					end
			end
		end
end)




1 Like

.value since my guess is that the thing you’re using is a class of value or whatever the name is.

1 Like

yep, as @bowlingfan21317 said, if it’s a valueObject, just add .Value to retrieve the value.

muscle.Value = math.clamp(muscle.Value, 0, 30)

Whenever you change muscle.Value, you need to put this after you change it. Otherwise, it’ll only happen once and not repeatedly as you need.

1 Like

Alright, then I’m gonna try that, nevermind

1 Like

Thank you so much, it worked. I tried so many complex solutions and then it was this simple!

2 Likes

No worries, glad we could help!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.