I'm not exactly sure where to post this, but since this is probably newbie and dumb, I thought I'd ask here. I followed the Wiki on 'Making a simple gamemode'. When I spawn, I get this error in the server console:
[code]ERROR: GAMEMODE:'PlayerInitialSpawn' Failed: gamemodes\scenerp\gamemode\init.lua:46: attempt to index local 'ply' (a nil value)[/code]
My init.lua
[lua]AddCSLuaFile( "cl_init.lua" ) //dl that
AddCSLuaFile( "shared.lua" ) //dl that too
AddCSLuaFile( "specialchars.lua" ) //and this
include( 'cl_init.lua' )
include( 'shared.lua' ) //load that
include( 'specialchars.lua' ) //This too
function GM:PlayerSpawn( ply ) //What happens when the player spawns
self.BaseClass:PlayerSpawn( ply )
ply:SetGravity( 0.75 )
ply:SetMaxHealth( 100, true )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 325 )
end
function GM:PlayerInitialSpawn( ply ) // When spawning after joining the sever
//CheckSpecialCharacters( ply ) //Is he defined in the specialchars.lua file? If yes, then he gets those values, and it stops here.
//if ply:IsAdmin() then //Is the connecting player admin?
//sb_team2( ply ) //If he is then set his team to team 2.
//else // If he isn't admin then,
joining( ply ) //Call the function joining (found near the bottom of this file)
concommand.Run( ply, "sb_start" )
//RunConsoleCommand( "sb_start" ) //Run the console command defined in cl_init.lua.
//end //Close the if
end //close the function
function GM:PlayerLoadout( ply ) // What should the player recieve when joining a team?
if ply:Team() == 1 then //If he is team 1, then give him the following items
ply:Give( "weapon_physcannon" ) // A Gravity gun
ply:Give( "weapon_physgun" ) // A Physics gun
ply:Give( "gmod_tool" ) // and don't forget the tool gun!
elseif ply:Team() == 5 then
ply:give( "weapon_physcannon" )
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
ply:Give( "weapon_buddyfinder" ) //donor weapon
end
end // End the function
function sb_team1( ply ) //This is what happens when we enter sb_team1 into the console.
ply:UnSpectate() //Since he was set to spectate until he presses the 'hell yeah' button, we now unspecatate him
ply:SetTeam( 1 ) //We set his team to one, a.k.a 'guest'
ply:Spawn() //Spawn the player
ply:PrintMessage( HUD_PRINTTALK, "[SceneRP] Welcome to SceneRP, " .. ply:Nick() .. "!" ) //Gives the message [SimpleBuild]Welcome to the server, (playername here)" in the talk area.
end //End the function.
concommand.Add( "sb_team1", sb_team1 ) //Now, we make sure that when we enter sb_team1 into console that it calls the function. This is KEY. Otherwise players won't be able to play.
function joining( ply ) // The function that's called when the player is not admin or a special character, at the top.
ply:Spectate( 6 ) //Set him to spectate in free-roam mode. He doesn't actually fly around, since he has a window open at this point.
ply:SetTeam( 4 ) //Set his team to Joining
end //End the function
[/lua]
Whats with all the // infront of all the functions 20-29?
[QUOTE=Willox;22802907]Whats with all the // infront of all the functions 20-29?[/QUOTE]
Not sure, I think I never set up a team2 function, and I wanted to try concommand.run instead of RunConsoleCommand or whatever.
On the server:
[b][url=http://wiki.garrysmod.com/?title=Player.ConCommand]Player.ConCommand [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
On the client:
[b][url=http://wiki.garrysmod.com/?title=G.RunConsoleCommand]G.RunConsoleCommand [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
If that doesn't work, perhaps the function is being called before the player is spawned? Try putting a timer on it and testing.
[QUOTE=Saint Devil;22803212]On the server:
[b][url=http://wiki.garrysmod.com/?title=Player.ConCommand]Player.ConCommand [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
On the client:
[b][url=http://wiki.garrysmod.com/?title=G.RunConsoleCommand]G.RunConsoleCommand [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b]
If that doesn't work, perhaps the function is being called before the player is spawned? Try putting a timer on it and testing.[/QUOTE]
Alright, thanks, the second one worked. But now I'm getting:
[code]gamemodes/scenerp/gamemode/init.lua:47: attempt to index local 'ply' (a nil value)[/code]
Can I see your more recent code?
[QUOTE=Banana Lord.;22804386]Alright, thanks, the second one worked. But now I'm getting:
[code]gamemodes/scenerp/gamemode/init.lua:47: attempt to index local 'ply' (a nil value)[/code][/QUOTE]
Do you have a team 1?
[QUOTE=Saint Devil;22805024]Can I see your more recent code?[/QUOTE]
Ok, no more errors, but now it just isn't running the sb_start command. And now, it's tell me it isn't even a command.
init.lua
[lua]AddCSLuaFile( "cl_init.lua" ) //dl that
AddCSLuaFile( "shared.lua" ) //dl that too
AddCSLuaFile( "specialchars.lua" ) //and this
include( 'cl_init.lua' )
include( 'shared.lua' ) //load that
include( 'specialchars.lua' ) //This too
function GM:PlayerSpawn( ply ) //What happens when the player spawns
self.BaseClass:PlayerSpawn( ply )
ply:SetGravity( 0.75 )
ply:SetMaxHealth( 100, true )
ply:SetWalkSpeed( 325 )
ply:SetRunSpeed( 325 )
end
function GM:PlayerLoadout( ply ) // What should the player recieve when joining a team?
if ply:Team() == 1 then //If he is team 1, then give him the following items
ply:Give( "weapon_physcannon" ) // A Gravity gun
ply:Give( "weapon_physgun" ) // A Physics gun
ply:Give( "gmod_tool" ) // and don't forget the tool gun!
elseif ply:Team() == 2 then
ply:give( "weapon_physcannon" )
ply:Give( "weapon_physgun" )
ply:Give( "gmod_tool" )
ply:Give( "weapon_buddyfinder" ) //donor weapon
end
end // End the function
function sb_team1( ply ) //This is what happens when we enter sb_team1 into the console.
//ply:UnSpectate() //Since he was set to spectate until he presses the 'hell yeah' button, we now unspecatate him
ply:SetTeam( 1 ) //We set his team to one, a.k.a 'guest'
ply:Spawn() //Spawn the player
welcomemessage( ply )
end //End the function.
function sb_team2( ply )
ply:UnSpectate()
ply:SetTeam( 2 )
ply:Spawn()
welcomemessage( ply )
end
function welcomemessage( ply )
ply:PrintMessage( HUD_PRINTTALK, "[SceneRP] Welcome to SceneRP, " .. ply:Nick() .. "!" )
end
concommand.Add( "sb_team1", sb_team1 ) //Now, we make sure that when we enter sb_team1 into console that it calls the function. This is KEY. Otherwise players won't be able to play.
//concommand.Add( "sb_team2", sb_team2 )
function joining( ply ) // The function that's called when the player is not admin or a special character, at the top.
ply:Spectate( 6 ) //Set him to spectate in free-roam mode. He doesn't actually fly around, since he has a window open at this point.
ply:SetTeam( 4 ) //Set his team to Joining
end //End the function
[/lua]
cl_init.lua
[lua]include( 'shared.lua' ) //LOAD THAT
timer.Create( "assignteam", 5, 1, StartSpawn())
function setteam()
//if ply:IsAdmin() == true then
//RunConsoleCommand( "sb_admin" )
RunConsoleCommand( "sb_team1" )
end
concommand.Add( "sb_start", setteam )
function GM:PlayerInitialSpawn( ply )
//joining( ply )
function StartSpawn()
RunConsoleCommand( "sb_start" )
end
end //close the function[/lua]
I now have this error: gamemodes/scenerp/gamemode/cl_init.lua:3:attempt to call global 'StartSpawn' (a nil value)
i think it's a dumb mistake
[editline]04:57PM[/editline]
[QUOTE=DocDoomsday;22805311]Do you have a team 1?[/QUOTE]
Yes, when I run the command manually it works.
To fix the error:
[lua]timer.Create( "assignteam", 5, 1, StartSpawn)[/lua]
The function arguments '()' are not needed when using a timer.
[editline]Hey there, sexy.[/editline]
Wait a sec, I just looked at your code. The InitialSpawn hook is serverside. And also, concommands are global, thus you can run the console command in the init.lua, rather than the cl_init.lua
Replace RunConsoleCommand with [code]LocalPlayer():ConCommand("sb_start")[/code] or whatever it is, =]
[QUOTE=SchumacherAlt;22806870]Replace RunConsoleCommand with [code]LocalPlayer():ConCommand("sb_start")[/code] or whatever it is, =][/QUOTE]
[code]You should not use ConCommand on LocalPlayer() as it is depreciated. You should instead use the RunConsoleCommand() global function. [/code]
Works perfectly fine for me, and RunConsoleCommand doesn't work on multiplayer for me, with my gamemodes.
It may work fine, but it is depreciated.
Well, it fixed the error I was having with gamemodes I made, and it seems to be the same error he is having, somehow it fixed it. Not sure, though.
Sorry, you need to Log In to post a reply to this thread.