This is not working for me.. Can someone help me?
[lua]
Commands = { [1] = { "frrp_citizen" },
[2] = { "frrp_officer" },
[3] = { "frrp_chief" },
[4] = { "frrp_mayor" },
[5] = { "frrp_gang" },
[6] = { "frrp_mob" },
[7] = { "frrp_gun" },
[8] = { "frrp_medic" } }
for k, v in pairs( Commands ) do
if ply:Team() == k then
for i, p in pairs( v ) do
RunConsoleCommand( p )
end
end
end
[/lua]
Try this: (I haven't tested) This must be done server-side
[lua]Commands = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k, v in pairs(Commands) do
if(ply:Team() == k) then
ply:ConCommand(v)
end
end
[/lua]
No, it's not working, but thanks for trying to help me.
I've edited the code a little... and this command is server-side or client-side ?
Also, the commands table indexes must coincide with the team indexes...
The command is server-side. Example: If I type frrp_gun in console, the player change team to gun.
[editline]13th February 2011[/editline]
It's still not working...
#1. Post a syntax error or whatever is going on in detail.
#2. It may not be working because "ply" is nil from what we can see. There is nothing defining "ply" on that code.
Try if player:Team() == k ?? I'm not really sure what you are trying to do. But what I saw from it, that may work.. Don't quote me on this because I'm sorta new too.
When you type in console "frrp_gun", your team is changed to gun?
[quote=InfernalCookie]It may not be working because "ply" is nil from what we can see. There is nothing defining "ply" on that code.[/quote]
I'm assuming he's using the code inside a concommand.Add("cmd", function(ply, cmd, args))
Error:
ERROR: GAMEMODE: 'PlayerInitialSpawn' Failed: [@gamemodes\noname\gamemode\init.lua:37] bad argument #1 to 'pairs' (table expected, got string)
Code:
[lua]
function GM:PlayerInitialSpawn( ply )
Commands = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k, v in pairs( Commands ) do
if ply:Team() == k then
for i, p in pairs( v ) do
RunConsoleCommand( p )
end
end
end
end [/lua]
Remove that second loop (for i, p in pairs(v) do). You do not define another table in the "Commands"-table, but a string instead. So just run the command (for the player, so ply:ConCommand(v)).
Well, go into your code and find line 37, because here we have no idea what line 37 is. BUT I'm assuming it is line 8. If I am not mistaken, that is finding the string of the players name. And it is in the forloop for the table, so it is asking for the table? But, RunConsoleCommand is running it for the entire server, including the players.
Edited code without errors (server- and client-side)
[lua] Commands = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k, v in pairs( Commands ) do
if ply:Team() == k then
ply:ConCommand( v )
end
end
end [/lua]
Yes, that should work. Although I am not sure what the console command of their own name would be and what it does.. But do what you're doing.
[QUOTE=Fruitwesp;28028355]Edited code without errors (server- and client-side)
[lua] Commands = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k, v in pairs( Commands ) do
if ply:Team() == k then
ply:ConCommand( v )
end -- Remove this
end
end [/lua][/QUOTE]
Remove the extra "end" at line 6
[QUOTE=Athos;28028636]Remove the extra "end" at line 6[/QUOTE]
Nope. It is all in a function.... Although if it isn't then I stand corrected. I noticed that too earlier but I had figured it was in a function.
Nop, still not working
[lua] Commands = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k, v in pairs( Commands ) do
if ply:Team() == k then
ply:ConCommand( v )
end
end
end [/lua]
[QUOTE=Athos]When you type in console "frrp_gun", your team is changed to gun?[/QUOTE]
[QUOTE=Athos;28029303]When you type in console "frrp_gun", your team is changed to gun?[/QUOTE] Yes, when I type it into console my team is changing to gundealer. Do I also need concommand.Add( "frrp_gun", SetTeam)???
Are you calling the code in the function GM:PlayerInitialSpawn() ?
if yes then probably (DarkRP?) spawns player with citizen team as default, and in such case, isn't better to just set the gundealer as the default team?
[QUOTE=Athos;28029499]Are you calling the code in the function GM:PlayerInitialSpawn() ?
if yes then probably (DarkRP?) spawns player with citizen team as default, and in such case, isn't better to just set the gundealer as the default team?[/QUOTE]
Yes, calling the function in GM:PlayerInitialSpawn(). Players spawns with citizen team as default and it is not based on DarkRP (making my own rp gamemode).
[editline]13th February 2011[/editline]
Gun Dealer is an example :P
so what exactly you want to do?
Ok, I don't wanna make a whole list of functions to make console commands.
I wanna use a for loop instead of the functions.
Example:
[lua]
function frrp_citizen()
ply:SetTeam( 1 )
end
function frrp_officer()
ply:SetTeam( 2 )
end
function frrp_chief()
ply:SetTeam( 3 )
end
function frrp_mayor()
ply:SetTeam( 4 )
function frrp_medic()
ply:SetTeam( 8 )
end
concommand.Add( "frrp_citizen", frrp_citizen )
concommand.Add( "frrp_officer", frrp_officer ) [/lua]
This?
[lua]concommand.Add("frrp_change_team", function(ply, cmd, args)
local team = args[1]
if(!team) then return ply:SetTeam(1) end -- if no arguments were given, assigns a default team
local teams = {"frrp_citizen","frrp_officer","frrp_chief","frrp_mayor","frrp_gang",
"frrp_mob","frrp_gun","frrp_medic"}
for k,v in pairs(teams) do
if(v == team) then
ply:SetTeam(k)
return
end
end
end)
[/lua]
Note: the teams table indexes must coincide with the real team indexes...
usage: frrp_change_team "frrp_gun"
Thanks a lot! Really! :D
guys this should have been solved a long time ago. You need to learn the fact that ply is just a variable, it's not part of lua you have to define it. If you are using a for loop and use k, v then v wil lbe the player so instead of using ply:Method() you use v:Method() unless you wanted to do for k, ply in pairs()
[lua]
local cmds = {}
cmds[1] = {"frrp_citizen","Citizen"}
function ChangePlayerTeam(ply)
if not teamchangedelay then teamchangedelay = 120 end -- set default team delay here
for a,b in pairs(cmds) do
concommand.Add(b[1],function()
local delay = teamchangedelay -- may not want to use global variables convar instead
if ((ply.lastchange + delay) > CurTime()) then ply:ChatPrint("You must wait "..math.Round(((ply.lastchange + delay) - CurTime())).." seconds.") return end
if ply:Team() == k then ply:ChatPrint("You are already a "..b[2]) return end -- you may want to use Notify() instead if you have the function
ply:SetTeam(a)
ply:ChatPrint("Successfully changed team to "..b[1])
for l,m in pairs(player.GetAll()) do m:ChatPrint(ply:Nick().." changed their job to "..b[2]) end -- NptifyAll() if you are using darkrp is good
ply.lastchange = CurTime()
end)
end
end
hook.Add("Initialize","AddTeamCommands",ChangePlayerTeam)
concommand.Add("adm_setteamdelay",function(ply,cmd,args) -- this will not save, good until crash/map change/restart
if not teamchangedelay then teamchangedelay = 120 end
if not ply:IsAdmin() then ply:ChatPrint("You do not have permission for this command.") end
teamchangedelay = args[1]
for s,d in pairs(player.GetAll()) do d:ChatPrint("Team delay set to "..teamchangedelay) end
end)
[/lua]
You may want to use Notify() instead of chat print but I am unsure if you have those functions if you need them just ask and I will post the ones from sandbox.
Also, you should edit the tables and delay to you're liking and to make it save use sql or file.append or whatever you do to save you're config.
Thanks again... and no I don't have the functions to use Notify(). You can also paste the functions?
Sorry, you need to Log In to post a reply to this thread.