how to check players online
#1

Whenever he comes with 0 players connected it says "uh, looks like the server is empty... except you" ?
Reply
#2

Next time search.
Here you go: https://sampforum.blast.hk/showthread.php?tid=168818
Reply
#3

Quote:
Originally Posted by RedRex
Посмотреть сообщение
How can you detected if there's 1 connected (showing message) and hide when there's 2 online?
Reply
#4

There is a full tutoiral you can check there bro . If u need a help just start new topic or countiue with this topic.
Reply
#5

Quote:
Originally Posted by severance
Посмотреть сообщение
How can you detected if there's 1 connected?
pawn Код:
stock GetOnLinePlayers()
{
    new OnLine;
    for(new i, g = GetMaxPlayers(); i < g; i++)
        if(IsPlayerConnected(i))
            OnLine++;
    return OnLine;
}
Why do you want to specifically know if there's 1 connected?
Reply
#6

Quote:
Originally Posted by Jing_Chan
Посмотреть сообщение
pawn Код:
stock GetOnLinePlayers()
{
    new OnLine;
    for(new i, g = GetMaxPlayers(); i < g; i++)
        if(IsPlayerConnected(i))
            OnLine++;
    return OnLine;
}
Why do you want to specifically know if there's 1 connected?
Because there's going to be a message shown if the player is alone in the server, and when the second player joins, the message should be hidden to next players. I want to understand how can you detect a certain amount of players from online list. Example: "5 players needed to start the gamemode" and then it hides the message and players can play.
Reply
#7

pawn Код:
GetOnlinePlayers();
if(OnLine == 1) ...
I believe that would work?
Reply
#8

Quote:
Originally Posted by Jing_Chan
Посмотреть сообщение
pawn Код:
GetOnlinePlayers();
if(OnLine == 1) ...
I believe that would work?
You're dead wrong, and don't throw out guesses like that again... Reason being that the GetOnlinePlayers function, isn't going to hand the variables it made, as they aren't global, to the rest of the script.

OnLine ONLY exists 'within' that function.

You use what it returns to point out what happened to the code that called it.
Код:
if(GetOnlinePlayers()>5){Commence}
else if{Don't}
^^ This check very likely doesn't actually work, but it's the idea that the actual variable isn't visible to the code at that level, and thus it should error out saying the variable isn't defined when you compiled it.
Reply
#9

Thanks Sew, your the one that actually explains something in that forum. I will try what i can and i hope it will work.
Reply
#10

You should try this. I have explained everything to you below.

PHP код:
/*
Script Documentation:
Made by: brauf
Tested: Yes
*/
#include <a_samp>
new PlayersOnline// this variable stores a integer of the players online
GetPlayersOnServer() // function to get players on server
{
    
PlayersOnline 0// reset online players each time when trying to fetch it otherwise "PlayersOnline" will just keep increasing each time (++) with each check
    
for(new iMAX_PLAYERSi++) // do a loop of all player slots available (1, 2, 3 - 1000), if player slot 1 or 2 or 3 has a valid player connected (IsPlayerConnected) then increase "PlayersOnline" by 1 (++)
    
{
        if(
IsPlayerConnected(i)) // a check to see if player is online on that id
        
{
            
PlayersOnline++; // increase "PlayersOnline" by 1 if there is a online player on
            
return PlayersOnline// return "PlayersOnline" which tells the server how much players are on
        
}
    }
    return 
PlayersOnline// return "PlayersOnline" which stores the value of how much players are on
}
public 
OnPlayerConnect(playerid)
{
    
GetPlayersOnServer(); // execute this function "OnPlayerConnect" so when a player logs in, it tries to detect how much players are on
    
if(PlayersOnline == 1// if online players = 1, then there is only 1 player online
    
{
        
SendClientMessage(playerid, -1"Looks like the server is empty, except you."); // send a message
    
}
    return 
1;

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)