how do you make a players online counter?

id: 462276

category: Help with Scripts

posts: 27

_THEBOSS_7 _THEBOSS_7 loading
i assume it is a simple script? just counting how many players are online w/cloud variables?
DerpyHead0 DerpyHead0 loading
TLDR: as just a player counter for your project, it's impossble. if this is for cloud multiplayer, keep reading

it's easy to detect when a player connects, but checking for a player disconnect is a lot more complicated, because it means the code stops running entirely. stop button detection won't work here either, since they could just close the page.

what you need is another player to keep track of the disconnected player, and for online players to constantly update a cloud variable. however, this maxes out at the amount of cloud variables you have (10 if i remember correctly) because multiple players sharing the same variable will cause issues as players with lag overwrite each other.

all this is fine for cloud multiplayer with a player limit, but just trying to use it to count any player on the project isn't possible.
This is actually possible, you just have a limit. Let's say the limit is 256, to simplify things. There must be one variable, called “tick”. If all 256 slots are full, it should look something like this.
0001011101101010101… and so on.
What we need is to be able to keep track of all of the digits at once. Your tick should be changing from 0 to 1 every 0.1 seconds, and there can be 256 clones to keep track of their individual tick. (I'd tell you how to use CloneID, but i'm assuming you're not a noob.)
They should wait 3 times, and check if all times are the same. They wait 0.1 seconds each time (obviously), so it should take 0.3 seconds to delete a player if they're inactive.
For example, if their recording is 000 or 111, that player is inactive and can be kicked out. But if it's 001 or 110 or 010 or such, that player can be ignored for now.
This is just a quick summary of how it works. Actually doing it would be considerably harder.
To measure the number of players, that's easy, just do length(allTicks).
DerpyHead0 DerpyHead0 loading

BlockGardens wrote:

This is actually possible, you just have a limit. Let's say the limit is 256, to simplify things. There must be one variable, called “tick”. If all 256 slots are full, it should look something like this.
0001011101101010101… and so on.
What we need is to be able to keep track of all of the digits at once. Your tick should be changing from 0 to 1 every 0.1 seconds, and there can be 256 clones to keep track of their individual tick. (I'd tell you how to use CloneID, but i'm assuming you're not a noob.)
They should wait 3 times, and check if all times are the same. They wait 0.1 seconds each time (obviously), so it should take 0.3 seconds to delete a player if they're inactive.
For example, if their recording is 000 or 111, that player is inactive and can be kicked out. But if it's 001 or 110 or 010 or such, that player can be ignored for now.
This is just a quick summary of how it works. Actually doing it would be considerably harder.
To measure the number of players, that's easy, just do length(allTicks).
that acutally doesn't work, because online players set the ENTIRE variable. so if someone that has bad internet joins, they'll keep overwriting the cloud variable with old data (which means overwriting everyone's ticks) and stop the ticks from working properly.

also, 256 clones just sitting in the background isn't a good idea, that would cause tons of unneccecary lag and leaves less room in the project for other clones.

DerpyHead0 wrote:

BlockGardens wrote:

This is actually possible, you just have a limit. Let's say the limit is 256, to simplify things. There must be one variable, called “tick”. If all 256 slots are full, it should look something like this.
0001011101101010101… and so on.
What we need is to be able to keep track of all of the digits at once. Your tick should be changing from 0 to 1 every 0.1 seconds, and there can be 256 clones to keep track of their individual tick. (I'd tell you how to use CloneID, but i'm assuming you're not a noob.)
They should wait 3 times, and check if all times are the same. They wait 0.1 seconds each time (obviously), so it should take 0.3 seconds to delete a player if they're inactive.
For example, if their recording is 000 or 111, that player is inactive and can be kicked out. But if it's 001 or 110 or 010 or such, that player can be ignored for now.
This is just a quick summary of how it works. Actually doing it would be considerably harder.
To measure the number of players, that's easy, just do length(allTicks).
that acutally doesn't work, because online players set the ENTIRE variable. so if someone that has bad internet joins, they'll keep overwriting the cloud variable with old data (which means overwriting everyone's ticks) and stop the ticks from working properly.

also, 256 clones just sitting in the background isn't a good idea, that would cause tons of unneccecary lag and leaves less room in the project for other clones.
Hm, I never thought about that. Maybe we can use Translate and forbid anyone with bad internet from joining? And make every clone handle 4 ticks rather than 1?
_THEBOSS_7 _THEBOSS_7 loading
guess its not as simple as i thought…. clones are no problem for me, lag sure, but i am not using clones.
DerpyHead0 DerpyHead0 loading

BlockGardens wrote:

Maybe we can use Translate and forbid anyone with bad internet from joining?
it would be almost impossible to check if someone is messing up other players ticks, and if this means preventing people from playing the game properly, it's a really bad idea.

_THEBOSS_7 wrote:

guess its not as simple as i thought…. clones are no problem for me, lag sure, but i am not using clones.
lag can prevent a lot of people with worse computers from playing the game, and again, preventing people from playing the game properly is a terrible idea.

what do you even need this online counter for? is it acutally important for the game, is the game multiplayer, or is it just something cool? because if it's not needed i don't think it's worth adding, if it's just some counter to try and show how popular the game is, there's lots of other things you can do, like a top score list, a counter for players that have beaten the game, etc.
_THEBOSS_7 _THEBOSS_7 loading
it's not a game, it's a reverse whitelisted chat. link; ( it broken rn ) https://scratch.mit.edu/projects/455826566/
DerpyHead0 DerpyHead0 loading
2 things:
1) this project is just a regular whitelist, you have whitelist and blacklist mixed up
2) online chats aren't allowed on scratch, even with the whitelist
_THEBOSS_7 _THEBOSS_7 loading
whitelists only stop certain words, this ONLY allows words in the list, so it is not technically an open chat.
( come to think of it you are prob. right, but this is not open chat, it's technically the same thing as looking at a ton of buttons and choosing what word to say, except u type )
DerpyHead0 DerpyHead0 loading

_THEBOSS_7 wrote:

whitelists only stop certain words, this ONLY allows words in the list, so it is not technically an open chat.
no, blacklists only stop certain words, and it's still an online chat. people can still put words together to insult people, give contact info, etc. it doesn't matter how you limit the words, until it gets to the point where it's not even a chat anymore.

the only thing i think is allowed is only being able to select from a list of possible messages, which then you can't really have any kind of conversation.
diggy5555 diggy5555 loading
um my game isn't working and i don't have much experience with online games game issue
_THEBOSS_7 _THEBOSS_7 loading

diggy5555 wrote:

um my game isn't working and i don't have much experience with online games game issue
1) make your own topic next time
2) can't help you, sorry.

DerpyHead0 wrote:

BlockGardens wrote:

Maybe we can use Translate and forbid anyone with bad internet from joining?
it would be almost impossible to check if someone is messing up other players ticks, and if this means preventing people from playing the game properly, it's a really bad idea.

_THEBOSS_7 wrote:

guess its not as simple as i thought…. clones are no problem for me, lag sure, but i am not using clones.
lag can prevent a lot of people with worse computers from playing the game, and again, preventing people from playing the game properly is a terrible idea.

what do you even need this online counter for? is it acutally important for the game, is the game multiplayer, or is it just something cool? because if it's not needed i don't think it's worth adding, if it's just some counter to try and show how popular the game is, there's lots of other things you can do, like a top score list, a counter for players that have beaten the game, etc.
Wait, what if we use a “temp” variable?
coooolboy55 coooolboy55 loading
its possible

variables

a cloud variable called max
a local variable called tcgf (times clicked green flag)

code

when green flag clicked

change max by 1

change tcgf by 1

if tcgf > 1 then

change max by -1

forever

reset timer

2nd code

when timer > 0

change max by -1

set tgcf to 0


when green flag clicked
change(☁ players)by (1)
coooolboy55 coooolboy55 loading
oops the tcgf variable is not local its global
coooolboy55 coooolboy55 loading
but it still works when its local
coooolboy55 coooolboy55 loading

IMAhomosapien wrote:

when green flag clicked
change(☁ players)by (1)

[/quote]

if we press the green flag more than one time it will glitch

also if you press the stop sign it wont remove anything
coooolboy55 coooolboy55 loading

IMAhomosapien wrote:

when green flag clicked
change(☁ players)by (1)


if we press the green flag more than one time it will glitch

also if you press the stop sign it wont remove anything
could you use the scratch blocks to re do that cde plz. I need code 2!
homewins homewins loading
The basic concept is set the PLAYERS variable to 0 and go through a loop, checking if each player is active. If so, you can change PLAYERS by 1.

There is probably a variable called “active” or something like that in each player clone that tells you if that player is active. If this is the case, just have a different sprite set PLAYERS to 0 and then add an
if <[active] > [0]> then //assumes "active" variable is greater than 0 when the player is active
change [PLAYERS v] by (1)
end

//in a different sprite that runs its code before the "Cloud players" sprite:
set [PLAYERS v] to [0]
maybe we can use the “current second” operator and always refresh the player count every second?
Calvin9901 Calvin9901 loading
when I start as a clone
delete this clone
delete this clone