Avoid value limit bypass

Hi there! I’m working on a box carrying system, and in this system I define a “Weight Limit” that is 4 . The value addition is counted by a NumberValue and controlled by a BoolValue, so my script start with something like this:
if Character.PlayerInfo.WeightCarrying.MaxWeight.Value == false then

This work well with small numbers addition like +1
But this can be easily bypassed by making something like this: +1, +2. The value currently is = 3, so it accept more additions, but the player can pick up a big box that has a value of 4 and then this woudl make the total value = 7

So, how could I fix this? Like, count the value before it’s added? And if it’s going to be bigger than the limit, the code doesn’t run.

I’m not sure whether I understood the problem correctly, but from what I think I understand, I would make a value called totalCarry which will be a value that adds all the weight together. Everytime a weight is added, it will be added to the totalCarry variable. Then I’d simply check every time whether it would be greater than 4 or not. I’m just scratching the surface since you’d probably need to check like this sometimes

local totalCarry = 0

if urObjectWeight + totalCarry > 4 then
 warn("Too much weight")
else
-- do whatever u want to do here, the weight is good.
 print("Added weight")
 totalCarry += urObjectWeight
end

Just an example, I hope this helps you understand a little bit.

1 Like

This is the same that this line of code is doing, right?

As it is controlled by this code:

local Weight = script.Parent.Value

script.Parent.Changed:Connect(function(weightValue)
	Weight = weightValue

	if Weight == 4 then
		script.Parent.MaxWeight.Value = true
	else
		script.Parent.MaxWeight.Value = false
	end
end)

There is mutliple issues with this:
1, The .Changed event returns a property, not the porperties value, So weightValue is in this case is Value not the the actual number, consider using :GetPropertyChangedSignal("Value") instead, Or make
Weight = Script.Parent.Value.

also consider changing the if statment:

if Weight == 4 then
to
if Weight >= 4 then

It shouldn’t matter, becuase it should never be more than 4, but you never know.

The code that @Dev_Simphony provieded is not the same as your if statment :
if Character.PlayerInfo.WeightCarrying.MaxWeight.Value == false then
, You code is only cheking if you are at max capasity or not, His code looks if the new value would be bigger than 4.

function TryAddObject(objectWeight)   --objectWeight is the weight of the objetc you try to pick up
local currentCarry = script.Parent.Value    ---The current amout being caried, in your case i belive it would be script.Parent.Value?, This depends on where the script is located

if objectWeight+ currentCarry> 4 then
 warn("Too much weight") --The amout would exeed 4, so we don't add the object
else
 print("Added weight")
script.Parent.Value += objectWeight  --Update the value holding you current Weight. again depending on where the script is located,

--Here you can do what you want to do when you add the object, Maybe destory the 3d model?

end

end
1 Like

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