Raised This Month: $ Target: $400
 0% 

Problems with Random player


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
eNz0
Member
Join Date: Feb 2010
Location: Lithuania
Old 05-21-2010 , 10:11   Problems with Random player
Reply With Quote #1

Hi. When i use random player code (from tutorial && testing with PodBot)
And any other code (in this example giving weapon)
It always gives weapon not for 1 player, but for all.

Code:
#include <amxmodx>
#include <fakemeta>
#include <hamsandwich>
#include <fun>


public plugin_init()
{
    register_plugin("Random Player", "0.1", "padilha007")
    RegisterHam(Ham_Spawn, "player", "bacon_spawn", 1);
}

public bacon_spawn()
{
    new player = GetRandomPlayer("a");
    give_item(player, "weapon_m4a1")
}

GetRandomPlayer(const flags[]="", const teamORname[]="")
{
    new players[32], pnum;
    get_players(players, pnum, flags, teamORname);
    
    return (pnum > 0) ? players[random(pnum)] : 0;
}

Last edited by eNz0; 05-21-2010 at 10:30.
eNz0 is offline
fezh
Veteran Member
Join Date: Dec 2008
Location: BANNED
Old 05-21-2010 , 10:18   Re: Problems with Random player
Reply With Quote #2

I'm lazy to read all of your code but remember to not using stocks to give items to players. Use give_item( ) from Fun module instead.
__________________
"There is no knowledge, that is not power"
fezh is offline
eNz0
Member
Join Date: Feb 2010
Location: Lithuania
Old 05-21-2010 , 10:30   Re: Problems with Random player
Reply With Quote #3

Ok, changed code. (same random player problem)
eNz0 is offline
DruGzOG
Veteran Member
Join Date: Nov 2007
Location: Unknown
Old 05-21-2010 , 10:42   Re: Problems with Random player
Reply With Quote #4

maybe checking if the user is alive.
__________________
DruGzOG is offline
Send a message via AIM to DruGzOG
Hx7r
Member
Join Date: Mar 2009
Old 05-21-2010 , 10:48   Re: Problems with Random player
Reply With Quote #5

https://forums.alliedmods.net/showthread.php?t=92049
Hx7r is offline
eNz0
Member
Join Date: Feb 2010
Location: Lithuania
Old 05-21-2010 , 10:53   Re: Problems with Random player
Reply With Quote #6

Quote:
Originally Posted by Hx7r View Post
Same problem with this (tryed 1h ago)
eNz0 is offline
xPaw
Retired AMX Mod X Moderator
Join Date: Jul 2008
Old 05-21-2010 , 12:13   Re: Problems with Random player
Reply With Quote #7

Why you give a weapon to random player whenever any player spawns..
__________________
xPaw is offline
mysticssjgoku4
Veteran Member
Join Date: Jan 2005
Location: Chicago Heights, IL
Old 05-21-2010 , 14:12   Re: Problems with Random player
Reply With Quote #8

I too question why you give random players weapons only when a person spawns.

Your code was giving an m4 to id0, which would not work anyhow, but to be safe I threw a check in there. I also threw a check in there to make sure the person is alive. Test this code, but honestly not sure if it will work, been years since I coded something like this in pawn.

Code:
#include <amxmodx> #include <engine> #include <fakemeta> #include <fun> #include <hamsandwich> public plugin_init() {     register_plugin("Random Player", "0.1", "padilha007")     RegisterHam(Ham_Spawn, "player", "bacon_spawn", 1); } public bacon_spawn() {     new player;     if((player = GetRandomPlayer("a")) > 0)     {         give_item(player, "weapon_m4a1");     } } GetRandomPlayer(const flags[]="", const teamORname[]="") {     new players[32], pnum;     get_players(players, pnum, flags, teamORname);         new selected = 0;     while( is_user_alive((players[(selected = random(pnum))])))     {         return players[selected];     }     return 0; }
__________________


Last edited by mysticssjgoku4; 05-21-2010 at 14:21.
mysticssjgoku4 is offline
Send a message via AIM to mysticssjgoku4 Send a message via MSN to mysticssjgoku4
eNz0
Member
Join Date: Feb 2010
Location: Lithuania
Old 05-21-2010 , 14:39   Re: Problems with Random player
Reply With Quote #9

Thanks guys, with your help, i made it work
Here is my final code (wepon giving is only example)

Code:
#include <amxmodx>
#include <engine>
#include <fakemeta>
#include <fun>
#include <hamsandwich>

new bool: g_random[33]
public plugin_init()
{
    register_plugin("Random Player", "0.1", "padilha007")
    RegisterHam(Ham_Spawn, "player", "bacon_spawn", 1);
}

public bacon_spawn()
{
    new player;
    if((player = GetRandomPlayer("a")) > 0)
    {
    g_random[player] = true
    set_task(0.0, "give_weapon")    
    }
}

public give_weapon(id)
{
    if(g_random[id])
    {
        give_item(id, "weapon_m4a1");        
    }
}

GetRandomPlayer(const flags[]="", const teamORname[]="")
{
    new players[32], pnum;
    get_players(players, pnum, flags, teamORname);
    
    new selected = 0;
    while( is_user_alive((players[(selected = random(pnum))])))
    {
        return players[selected];
    }
    return 0;
}
eNz0 is offline
mysticssjgoku4
Veteran Member
Join Date: Jan 2005
Location: Chicago Heights, IL
Old 05-21-2010 , 14:45   Re: Problems with Random player
Reply With Quote #10

You are welcome, but I don't even see how that code would work :S. you are setting a task, which is, I am assuming, passing 0 to the id of give_weapon. Weird
__________________

mysticssjgoku4 is offline
Send a message via AIM to mysticssjgoku4 Send a message via MSN to mysticssjgoku4
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:38.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode