attempt to call method 'Team' (a nil value) -- Specific bug
3 replies, posted
Hello, first of all, I know there were lots of differents threads about my bug, but this time it's getting really weird and I'm sorry if I can't properly indent the code, I am new here
I'm trying to edit the code of the gamemode Prophunters of Mechanical Mind (crediting just in case)
I want to edit this function :
function GM:SwapTeams()
for k, ply in pairs(player.GetAll()) do
if ply:Team() == 2 then
ply:SetTeam(3)
elseif ply:Team() == 3 then
ply:SetTeam(2)
end
end
local ct = ChatText()
ct:Add("Teams have been swapped", Color(50, 220, 150))
ct:SendAll()
end
So I modified it properly and as a result I had
function GM:SwapTeams()
local ply = player.GetAll()
if ply:Team() == 3 then
ply:SetTeam(2)
end
local randomply = table.Random(player.GetAll())
randomply:SetTeam(3)
local ct = ChatText()
ct:Add( randomply .. " est maintenant le Hunter !", Color(255, 187, 51))
ct:SendAll()
end
But after that, when I launch my dedicated server and get to the end of a round, the console keeps spamming with
[ERROR] addons/prophunters_260275546/gamemodes/prophunters/gamemode/sv_teams.lua:86: attempt to call method 'Team' (a nil value)
1. SwapTeams - addons/prophunters_260275546/gamemodes/prophunters/gamemode/sv_teams.lua:86
2. RoundsThink - addons/prophunters_260275546/gamemodes/prophunters/gamemode/sv_rounds.lua:357
3. unknown - addons/prophunters_260275546/gamemodes/prophunters/gamemode/init.lua:84
Lines 2 and 3 aren't necessary to understand I think.
What I don't understand is why the ply:Team is causing trouble, could someone explain me how to fix that ?
The only workaround I found is to keep the loop
for k, ply inpairs(player.GetAll)) doif ply:Team() == 2then
I'm trying to get rid of that loop (modifying teams infinitely is quite weird)
Could someone help me with this ? Tell me if I need to post the whole code, I didn't wanted to at first because it would make a real mess in 1 post
Player/Team is used on player data, not an entire table, you need the loop so you can select individual players to do your function on
--use this
local ply
for k,v in pairs(player.GetAll() ) do
ply = v
end
if ply:Team() == 3 then
ply:SetTeam(2)
end
Thank you for the explanations ! It seems to be working now, I just need to rewrite the code to modify some things. Thanks a lot !
Sorry, you need to Log In to post a reply to this thread.