Stopping a value from adding

Hey! I want to make a backpack system so when the ‘currency’ value stops when it’s equal to the ‘backpack’ capacity. But I can’t understand how the value can go further then 10 because of the if statement should only go through if Shine2 is less or equal to 10. But the value won’t stop adding.

This is what’s happening
Skärmbild (58)

And this is the script a have made

local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild("leaderstats")
local Shine = leaderstats:FindFirstChild("Shine")
local Backpack = script.Parent.Parent.Value
local Shine2 = Shine.Value
if Shine2 <= 10 then
	script.Parent.Text = Shine.Value.."/".. Backpack.Value
	Shine.Changed:connect(function()
		script.Parent.Text = Shine.Value.."/".. Backpack.Value
		end)
end

I have just tried scripting some more but I couldn’t fix it.

1 Like

Change <= to <

The code executes even if it is equal to 10. You want to exclude 10.

Shine2 = Shine.Value will return the current value of Shine, and will not update when it is changed. You already have the Shine IntValue with the variable Shine, so compare it’s current value agaisnt the max value.

local Shine = leaderstats:FindFirstChild("Shine") -- keep this. remove Shine2
Shine.Value < 10 then
     -- stuff
end
1 Like

My script looks like this now

´´´lua
local player = game.Players.LocalPlayer
local leaderstats = player:WaitForChild(“leaderstats”)
local Shine = leaderstats:FindFirstChild(“Shine”)
local Backpack = script.Parent.Parent.Value
if Shine < 10 then
script.Parent.Text = Shine.Value…“/”… Backpack.Value
Shine.Changed:connect(function()
script.Parent.Text = Shine.Value…“/”… Backpack.Value
end)
end
´´´

Now it won’t update at all

You forgot to compare the value.

Change to if Shine.Value < 10 then

It still updates over the capacity 10

Picture

Is there a way you can prevent it from going over 10, for example, if Shine2 < 10 then Shine2.Value = 10?

I just noticed that your Shine.Changed function is inside of the Shine check. This would mean that whats happening is that your script will check the value one time when the script starts, but never again when the value changes.

Replace

with

script.Parent.Text = Shine.Value.."/".. Backpack.Value
Shine.Changed:Connect(function()
	if Shine.Value < 10 then
		script.Parent.Text = Shine.Value.."/".. Backpack.Value
	end
end)

Wouldn’t that just change the text to Shine.Value in the textbox? That says if the value is over 10 then set the text to the value over 10.

Instead of doing that, you can use math.clamp:

script.Parent.Text = math.clamp(Shine.Value, 0, Backpack.Value)."/".. Backpack.Value

This will make it so that the value will not go over the max storage for the backpack, while not going under 0.

1 Like