How to add class limit?

I’ve got this free model class system. You click the button, it gives you the tools, tadah. But I want to be able to add a class limit. Kinda like operation overlord. I’ve got this basic idea, but I have no idea how to implement it since my scripting level is extremely bad (hence using a free model).

So, when a player clicks on the button and gets given the tools, a script inside the button changes a value inside the same button to 2. The same script then detects if the value is either 0, 1 or 2. If the value is 2, then the button cannot be pushed. I just need some help on figuring out how to detect if the value is either 1 or 2 then making that do something

A script inside the ImageButton (Server Sided)

local imgBtn = script.Parent
local limitNum = 3

imgBtn.MouseButton1Click:Connect(function()
    if limitNum >= 3 then
       -- Give tools

      limitNum -=  1
    end
end)

This doesn’t look right. By the logic, it’ll only work once. If limitNum is greater than or equal to 3, it will give the tool once, then decrement limitNum by one. This no longer checks true for the if statement, as it would become 2 (which is not greater than or equal to 3).
Would look a bit more like this:

local imgBtn = script.Parent
local limitNum = 3 -- Change regarding how many times you want the tool to work
local counter = 0;

imgBtn.MouseButton1Click:Connect(function()
    if limitNum >= counter then
       -- Give tools

      counter += 1;
    end
end)

Of course, this will only be client-sided (unless you have a ServerScript inside the button [which works!]). In order to make it serversided, you will need to utilize RemoteEvents. No point in re-inventing the wheel to explain how RemoteEvents work, so check this out. If you have more questions, Google them, and if you are unable to arrive at an answer, let me know.

That’s what the OP said duh. And I gave him pseudo code, he is free to modify it any way he want, but in any way the exact words of the OP were to make a script that wouldn’t give tools if it was 0, 1 or 2, and by any means he can edit the code.

Your script also has multiple problems, and you do NOT need to utilize remote events to make this serversided. Putting this same exact script into a server script will work perfectly fine. I assume you would mean to add to the counter and not the limitNum variable, and also use <=, not >=.

Assuming your limitNum variable meant the max amount of times they can be given the item, your pseudo-code doesn’t represent that accurately. If not, your variable name is very misleading.

I really don’t know whether naming variable has any fault here, but if you find it misleading i will oblige to changing the name, however I must point out that using a RemoteEvent is entirely unwanted here.


Did you even read the question?
First and foremost, your variable. The name limitNum implies that there is a limit of limitNum times that a class can be given to a player. This is misleading considering what it actually serves to do. Regardless, there is a more efficient way to do it if you had planned on making it with a set 1-time hard limit (using a boolean rather than a number).

Second, RemoteEvents. They are absolutely wanted if it is a multiplayer game and the OP wants the class given to the player to replicate to other players. Tools given on the client DO NOT replicate to the server when equipped. The animation plays, yes, but the tool itself never appears.

I don’t see any point arguing, we will just let OP pick the best one.

Have a good day m8 :smiley:

Counter starts at 0. It would be foolish (and incorrect) to use <= when comparing it to the upper limit.

Yes, I understand using a serverscript would work. I did mention this in my original response. However, you must realize there are issues with doing that. For the sake of readability, I will let this post explain it.

EDIT: Good catch. I intended to add to the counter, and not the limitNum.

1 Like

No, it would not be incorrect at all to use <=. you should always use <= to tell, as it means if something is LESS or EQUAL to something. Right here, you are currently only adding to the counter IF the counter is GREATER than limitNum, which would always be false. 0 is never greater than 3.

Oops, misread my answer lol. Apologies, I meant to put counter on the right. I’ve edited it to reflect this change.