i made some changes to your code and explained them
PHP Code:
#include <amxmodx>
#include <cstrike>
#include <hamsandwich>
#define DEFAULT_MODEL 1
enum _:Models
{
Name[20],
NameModel[20],
RUTA[126],
FlagAdmin[6]
}
// creating a global variable to store user selected model
// here you must put the g_eModels model id starting from 0 to how many models do you have
new g_iUserModel[MAX_PLAYERS + 1];
new const g_eModels[][Models] =
{
//changed the flag as a string (you must use read_flags to transform it in bytes and use get_user_flags)
{ "hutao", "hutao", "models/player/hutao/hutao.mdl" , "a" },
{ "spiderman", "admin_spiderman", "models/player/donald/admin_spiderman.mdl", "b"},
{ "woailuo", "flash_woailuo", "models/player/donald/flash_woailuo.mdl", "c"}
}
public plugin_init()
{
register_clcmd("say /test", "ModelMenu")
RegisterHam(Ham_Spawn, "player", "fwPlayerSpawn", true) //true means the forward will be called after the player is spawned
}
public plugin_precache()
{
for(new i; i < sizeof(g_eModels); i++)
{
//better check if the model exists so the server won't crash
//doesn't make any sense here but it s a good practice
if(file_exists(g_eModels[i][RUTA]))
{
precache_generic(g_eModels[i][RUTA])
}
else
{
log_amx("Model ^"%s^" does not exists", g_eModels[i][RUTA])
}
}
}
public client_connect(id)
{
// reset user model to -1 that means no model or default_model id on user client id so the player that join the server won't have the previous user selected model
g_iUserModel[id] = DEFAULT_MODEL
}
public fwPlayerSpawn(id)
{
//check if player is alive and has a selected model
//you must check if user is alive even if you create the forward where the post parameter is true
if(is_user_alive(id) && g_iUserModel[id] > -1)
{
static iModel
iModel = g_iUserModel[id]
cs_set_user_model(id, g_eModels[iModel])
}
}
public ModelMenu(id)
{
new iMenu = menu_create("Models Menu", "ModelsHandler")
new szMenuItem[128];
for(new i; i < sizeof(g_eModels); i++)
{
//this is just to add [#] to the selected item so the player will know what skin he's using
formatex(szMenuItem, charsmax(szMenuItem), "%s%s", g_eModels[i][Name], (g_iUserModel[id] == i) ? "\y[\d#\y]" : "")
// didn't event need to pass the flag access
menu_additem(iMenu, szMenuItem)
}
//i'm checking if the user is connected to display the menu because without you will get an console error if the player leaves the server and the menu is opened
if(is_user_connected(id))
{
menu_display(id, iMenu, 0)
}
}
public ModelsHandler(id, iMenu, iItem){
if(iItem == MENU_EXIT)
{
menu_destroy(iMenu);
return PLUGIN_HANDLED;
}
//doesn't even need to pass the flag
if(!(get_user_flags(id) & read_flags(g_eModels[iItem][FlagAdmin])))
{
client_print(id, print_chat, "You don't have the access to use ^"%s^" model", g_eModels[iItem][Name])
ModelMenu(id)
return PLUGIN_HANDLED
}
cs_set_user_model(id, g_eModels[iItem][NameModel])
client_print(id, print_chat, "Selected Model: %s", g_eModels[iItem][Name])
g_iUserModel[id] = iItem
return PLUGIN_CONTINUE
}