Item bypassed my Capacity Limit

I really do not know how to explain this but I will try:

Let us say that you have an item that gives you 2 points every time you click
Once you reached 10 it no longer gives you points
Now let us say that you have an item that gives you 6 points every time you click
If you click twice it will exceed the limit and continues to give you points

Script:

local plr = game.Players.LocalPlayer

script.AddStrength.OnServerEvent:Connect(function(player)
	if player.leaderstats.Strength.Value == script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
	else if player.leaderstats.Strength.Value < script.Capacity.Value then
			game.Workspace.MainEvent.Disabled = false
		end
	end
end)

Where are you setting script.Capacity.Value.
Also, shouldn’t you be checking script.capacity.Value.Value for the actual number?

I am not sure what do you mean by “Setting”… I did check the actual value and it is what I wanted it to be

You said you had the value set to 10, I just wondered how you managed to set it to that value.

For troubleshooting the script you could put a print(player.leaderstats.Strength.Value) and print(script.Capacity.Value) statement in the if and elseif sections to see what it is at that point.

If you want to stop the count of MainEvent and disable it when the Strength value is equal or higher than capacity then you shouldn’t check if it is only equal to. Because your value goes above capacity it’s not going to be equals to capacity if it overpasses the limit. Try this:

local plr = game.Players.LocalPlayer

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

It still exceeds the limit but it stopped you from getting any more points once you’re over it

In this case if they have 9 points and they’re awarded at least 2 points then they would be above 10.

Make a check inside the thread where it disables the Event and change it to the max.

It stopped printing messages once I’m over the limit

So you want strength to be set as capacity once is above the limit?

local plr = game.Players.LocalPlayer

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

It worked! But when I tested this on an Item that gives more than the limit (Lets say 15) it still exceeds it but if you click again it deducts it back to the limit…

I could try making the limit a bit bigger and only make items that gives points which is a multiple of that limit… Should I wait for a better solution or just do this?

What about if you add Changed event on the Strength value and then change it to the limit? Try this:

local plr = game.Players.LocalPlayer

player.leaderstats.Strength.Changed:Connect(function()
    if player.leaderstats.Strength.Value >= script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
        player.leaderstats.Strength.Value = script.Capacity.Value
    end
end)

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

It says “attemp to index nil with ‘leaderstats’” on the output

Instead of ‘player.leaderstats’ type ‘plr.leaderstats’ on the changed event.

plr.leaderstats.Strength.Changed:Connect(function()
    if plr.leaderstats.Strength.Value >= script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
        plr.leaderstats.Strength.Value = script.Capacity.Value
    end
end)

i tried it and it still gives me the error

Try this one:

local plr = game.Players.LocalPlayer
plr:WaitForChild("leaderstats",10)

plr.leaderstats.Strength.Changed:Connect(function()
    if plr.leaderstats.Strength.Value >= script.Capacity.Value then
		game.Workspace.MainEvent.Disabled = true
        plr.leaderstats.Strength.Value = script.Capacity.Value
    end
end)

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

attemp to index nil with ‘WaitForChild’

Are you on script or local script? Because you can’t define local player on server script.

Try checking by adding up the “value to add” and the “value at the moment” first then checks if it exceeds the max value.

An example would be:

if (the_current_value + value_to_add) > max_value then
--do your thing.
end

Oops explained it incorrectly.