cl_init.lua ply:Nick(), ply:SetTeam(), etc. are not working.
4 replies, posted
Hey everyone, recently I've been having an issue with a gamemode I'm creating to practice Lua. I've created a piece of code that prints the name of the player and the team that they have joined upon joining that team, so in example it is supposed to work like this, "Bman99 has joined team Counter Terrorists!". I've asked other people and they said it is a problem with the player not being valid. I am looking for the solution to the valid problem and the ply:Nick(), ply:SetTeam(), etc. not working.
cl_init.lua
[lua]
include( 'shared.lua' )
function jointeammessageforcts(ply)
local ply = LocalPlayer()
chat.AddText(Color(51,51,255),ply," has joined team ",team.GetName(1))
end
usermessage.Hook("ctjoinmessage", jointeammessageforcts)
function jointeammessageforts(ply)
local ply = LocalPlayer()
chat.AddText(Color(255,0,0),ply," has joined team ",team.GetName(2))
end
usermessage.Hook("tjoinmessage", jointeammessageforts)
[/lua]
init.lua
[lua]
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )
function GM:PlayerInitialSpawn(ply)
ply:Spawn()
ply:SetModel("models/kleiner.mdl")
ply:Give("weapon_357")
umsg.Start("chooseTeam", ply)
umsg.End()
if team.NumPlayers(1) > team.NumPlayers(2) then
ply:SetTeam(2)
elseif team.NumPlayers(2) > team.NumPlayers(1) then
ply:SetTeam(1)
else
ply:SetTeam(math.random(1,2))
end
if ply:Team() == 1 then
umsg.Start("ctjoinmessage",ply)
umsg.End()
elseif ply:Team() == 2 then
umsg.Start("tjoinmessage",ply)
umsg.End()
end
end
[/lua]
This is what it looks like in chat.
[IMG]http://i.imgur.com/CAwGgc2.png[/IMG]
Thank you in advance!
[code]function GM:PlayerInitialSpawn(ply)
ply:Spawn()
ply:SetModel("models/kleiner.mdl")
ply:Give("weapon_357")[/code]
I'd recommend editing it to this.
[code]function GM:PlayerInitialSpawn(ply)
if ( Ply:IsValid() ) then
ply:Spawn()
ply:SetModel("models/kleiner.mdl")
ply:Give("weapon_357")
[/code]
[QUOTE=Kirkwhy;44536194][code]function GM:PlayerInitialSpawn(ply)
ply:Spawn()
ply:SetModel("models/kleiner.mdl")
ply:Give("weapon_357")[/code]
I'd recommend editing it to this.
[code]function GM:PlayerInitialSpawn(ply)
if ( Ply:IsValid() ) then
ply:Spawn()
ply:SetModel("models/kleiner.mdl")
ply:Give("weapon_357")
[/code][/QUOTE]
Thanks for the help, but that did not work, it still says "NULL has joined Counter Terrorists".
You probably shouldn't be calling ply:Spawn() in a Spawn function... they're already spawning.
Problem is solved.
Sorry, you need to Log In to post a reply to this thread.