How would I use table.Random(or math.random) to automatically set a player's team upon their first s
9 replies, posted
Hello. I'm making a gamemode that is team-based. I have everything but one feature ready; selecting the player's team upon their first spawn. I've never really been that familiar with table.Random and math.random, so I need some help getting this to work. BTW, here's the spawn function I'm using:
[CODE]function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )[/CODE]
Thanks.
[lua]
local teams = {1,2,3} -- Team indexes, use the globals you should have defined when setting up teams
function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:SetTeam( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
[/lua]
Like this.
Thanks!
Try this:
[code]
--[[Put the teams you want to select from here. Each will have an equally random chance of occuring.
I use a table so you can include only whichever teams you want.
Note that these are just global variables for the team indexes; you can also just put the numbers here instead and it will still work fine.]]
local StartTeams = {TEAM_INDEX_ONE, TEAM_INDEX_TwO}
-- Since this is a sequential table, you can just use math.Random to select a random index from the start of the table to the end.
ply:SetTeam(StartTeams[math.random(1, #StartTeams)])
[/code]
[B]Edit:[/B]
I had this up on a non-refreshed window and I decided to chip in. James did it first.
[QUOTE=James xX;47559556][lua]
local teams = {1,2,3} -- Team indexes, use the globals you should have defined when setting up teams
function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:SetTeam( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )
[/lua]
Like this.[/QUOTE]
Huh? It won't work. I have a system set up so that it says "Changed to Combine" or "Changed to Rebels" if you change your team, and the message doesn't show up when I spawn. Hmm, do I need to use an index instead of a number? I didn't set an index like TEAM_COMBINE, but I did set up numbers. I think a simpler way would be to run the console commands I made for setting teams instead(they're called set_team and set_team2), but I also need help getting that to work :P.
Anyways, my error search was unsuccessful, these are the two codes I'm using:
(in init.lua)
[CODE]local teams = {1,2} -- Team indexes, use the globals you should have defined when setting up teams
function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:Freeze( true )
ply:SetTeam( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )[/CODE]
and
(in shared.lua)
[CODE]function GM:CreateTeams()
team.SetUp(1, "Combine", Color(255, 0, 0), true)
team.SetSpawnPoint( Combine, ( "info_player_terrorist" ) )
team.SetUp(2, "Rebels", Color(61, 87, 106), true)
team.SetSpawnPoint( Rebels, ( "info_player_counterterrorist" ) )
end[/CODE]
[QUOTE=A Fghtr Pilot;47559948]Huh? It won't work. I have a system set up so that it says "Changed to Combine" or "Changed to Rebels" if you change your team, and the message doesn't show up. Hmm, do I need to use an index instead of a number? I didn't set an index like TEAM_COMBINE, but I did set up numbers.
Anyways, my error search was unsuccessful, these are the two codes I'm using:
(in init.lua)
[CODE]local teams = {1,2} -- Team indexes, use the globals you should have defined when setting up teams
function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:Freeze( true )
ply:SetTeam( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )[/CODE]
and
(in shared.lua)
[CODE]function GM:CreateTeams()
team.SetUp(1, "Combine", Color(255, 0, 0), true)
team.SetSpawnPoint( Combine, ( "info_player_terrorist" ) )
team.SetUp(2, "Rebels", Color(61, 87, 106), true)
team.SetSpawnPoint( Rebels, ( "info_player_counterterrorist" ) )
end[/CODE][/QUOTE]
Indexes are numbers.
Where is your code that prints "changed to x", because I'm going to guess that it's not getting called, because we are setting the team directly, instead of going down your function pathway.
[QUOTE=James xX;47559980]Indexes are numbers.
Where is your code that prints "changed to x", because I'm going to guess that it's not getting called, because we are setting the team directly, instead of going down your function pathway.[/QUOTE]
[CODE]--Sets the players team to the first argument when writing "set_team" into the console and respawns the player afterwards, ex. "set_team 1".
function ChangeMyTeam( ply, cmd, args )
ply:SetTeam( args[1] )
ply:Spawn()
PrintMessage( HUD_PRINTTALK, "Changed to the Combine." )
ply:SetModel("models/player/combine_soldier.mdl")
end
concommand.Add( "set_team", ChangeMyTeam )[/CODE]
Oh, wait, it might be changing the team, actually. However, I think we should just call my console commands( set_team and set_team2 ), as it would be a lot easier for me to tell if I actually switched them. My bad, it probably was changing the teams, but I think the console commands would be better. However, it may have not been changing the teams, as the spawns I set for the teams(info_player_counterterrorist and info_player_terrorist) aren't working(as in when I switch to the team that had those spawnpoints set to that team only, I wouldn't spawn there).
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/ConCommand]Player:ConCommand[/url], or you could write a function that changes the players team that would be used throughout your gamemode, and add on the concommand afterwards.
[QUOTE=James xX;47560019][img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Player/ConCommand]Player:ConCommand[/url], or you could write a function that changes the players team that would be used throughout your gamemode, and add on the concommand afterwards.[/QUOTE]
[CODE]local teams = {set_team,set_team2} -- Team indexes, use the globals you should have defined when setting up teams
function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:Freeze( true )
ply:ConCommand( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn )[/CODE]
[QUOTE=A Fghtr Pilot;47560042]local teams = {set_team,set_team2} -- Team indexes, use the globals you should have defined when setting up teams
[CODE]function FirstSpawn(ply)
timer.Simple(45, start ) -- after 180 seconds, start() is called.
ply:SetNWString("canusemenu", "yes")
ply:GodEnable()
ply:Freeze( true )
ply:ConCommand( table.Random( teams ) )
end
hook.Add( "PlayerInitialSpawn", "playerInitialSpawn", FirstSpawn[/CODE][/QUOTE]
local teams = {"set_team 1" , "set_team 2"}
But as I said, don't do it this way.
Sorry, you need to Log In to post a reply to this thread.