Item bypassed my Capacity Limit

nope I am not using a localscript

I still get an ā€œattempt to index nil with ā€˜leaderstatsā€™ā€ error and I am using a Script not a LocalScript

Thatā€™s because it cannot find the Player then. Try checking why the player variable is nil.

Also, game.Players.LocalPlayer isnā€™t accessible by normal Scripts and will always return nil.

You can use math.clamp() for this (if over max, returns max)

script.AddStrength.OnServerEvent:Connect(function(player)
	if player.leaderstats.Strength.Value >= script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
	elseif player.leaderstats.Strength.Value < script.Capacity.Value then
	    game.Workspace.MainEvent.Disabled = false
	end
    player.leaderstats.Strength.Value = math.clamp(player.leaderstats.Strength.Value, 0, script.Capacity.Value)
end)

Iā€™m not sure how youā€™re supposed to do this but it didnā€™t work

script.Parent.MainEvent.AddStrength0.OnServerEvent:Connect(function()
	if plr then
		if (game.Players.LocalPlayer.leaderstats.Strength.Value + 3) > script.Capacity then
			script.Parent.MainEvent.Disabled = true
			plr.leaderstats.Strength.Value = script.Capacity.Value
		else if (game.Players.LocalPlayer.leaderstats.Strength.Value + 3) < script.Capacity then
				script.Parent.MainEvent.Disabled = false
			end
		end
	end
end)

AddStrength0 is the Event that my item fires whenever you click
the code on line 2 solved the ā€œattempt to index nil with ā€˜leaderstatsā€™ā€ error

it sometimes work and sometimes it donā€™tā€¦

Is the Capacity object a NumberValue? If so, you are comparing a number to an instance. Do script.Capacity.Value instead.

1 Like

i did try this and nope it still not workingā€¦

1 Like

Are there any errors in the output?

there are no errorsā€¦ there are no errors on the outputā€¦

Use math.min() on the added values. Just enter the capacity as an argument.

For example:

local valX, valY = 7, 8
local cap = 10

local resultVal = math.min(valX + valY, cap) -- 10

-- 

local valX, valY = 2, 5
local cap = 10

local resultVal = math.min(valX + valY, cap) -- 7

you can just clamp, this article explains it ā†’ math | Roblox Creator Documentation

It does not workā€¦ It still exceeds the limit but deducts it back to the limit after I click for the second time

script.AddStrength.OnServerEvent:Connect(function(player)
	if player.leaderstats.Strength.Value >= script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
	elseif player.leaderstats.Strength.Value < script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = false
	end
	player.leaderstats.Strength.Value = math.clamp(player.leaderstats.Strength.Value, 0, script.Capacity.Value)
end)

How do you do this? Iā€™m not that advance yet

Wherever youā€™re adding the values, you should use math.min() like I did in those two examples. math.min returns the smallest of the values you pass in. Because of this, we can essentially limit the output of whatever number youā€™re inserting. If I want the player to be able to gain coins, but not be able to have more than 100 at a time, then I would write something like:

local resultVal = math.min(playerCoins.Value + 5, 100) -- for the purpose of demonstration, each coin pickup gives 5 coins

If the player has 98 coins when picking up the coin, then playerCoins.Value + 5 would be 103. But we passed 100 into math.min, which is smaller than 103. Therefore, resultVal will be equal to 100.

2 Likes

If your problem has been solved, please mark the solution!

I donā€™t know if this is correct or not but when I click it gives 10 (The capacity of my backpack) rather than what it was scripted to give ( 1 or 3 with gamepass)

player.leaderstats.Strength.Value = math.min(script.Capacity.Value + 3, 10)

Since this is starting to get hopeless I decided to scrap the backpack Idea

Look at what you wrote. You put script.Capacity.Value, which I assume is equal to ten. Thatā€™s always going to return 13. You need to pay more attention to what youā€™re writing before you throw in the towel on things like this.

1 Like

Oh my god bro I already deleted a lot of stuff relating to the backpackā€¦