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.
i did try this and nope it still not workingā¦
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
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.
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.
Oh my god bro I already deleted a lot of stuff relating to the backpackā¦