*Help* Issue with GM:PlayerInitialSpawn

Hi guys I have an issue I have been trying to work on this for like 4 hours now and I can’t figure out the issue. Perhaps one of you can help me.

So I want to randomly put a player on a team, which works fine. But then I want to add an if statement so if they are on that team send this net message. But this doesn’t seem to work. If anyone can help me I would be must appreciative!




--ply:SetTeam( 1 )--Set the team to contenders.
	local spawn = {1,11,12,13,14,15} 
	ply:SetTeam( spawn[math.random(1,#spawn)] )--Set the team to contenders.
	
	 util.AddNetworkString( "spawn2" )
	 net.Start("spawn2")
     net.WriteString("some text")
     net.Send(ply)

if spawn == 1 then --Contender 	
 util.AddNetworkString( "contender" )
	 net.Start("contender")
     net.WriteString("some text")
     net.Send(ply)	
	 end
if spawn == 11 then --Mr.sLayer
	 util.AddNetworkString( "slayer" )
	 net.Start("slayer")
     net.WriteString("some text")
     net.Send(ply)	
	end
if spawn == 12 then --Hunter
	 util.AddNetworkString( "hunter" )
	 net.Start("hunter")
     net.WriteString("some text")
     net.Send(ply)	
	end
if spawn == 13 then --Liberator
	 util.AddNetworkString( "lib" )
	 net.Start("lib")
     net.WriteString("some text")
     net.Send(ply)	
	end
if spawn == 14 then --Abominator
	 util.AddNetworkString( "abominator" )
	 net.Start("abominator")
     net.WriteString("some text")
     net.Send(ply)	
	end
 if spawn == 15 then --Deadman
	 util.AddNetworkString( "deadman" )
	 net.Start("deadman")
     net.WriteString("some text")
     net.Send(ply)	
	end



Thanks

-Duby

Make sure at the top of the file you have put
[lua]until.AddNetworkString(“Name of msg”)[/lua]
For each one you have added.

It has to be added at least 3 seconds before the message is sent so where they are now will just give you an error.

didn’t make a difference, it still doesn’t work. :L

I have been at this for hours and hours now and I can’t find a solution.

you don’t need that many net requests

Doesn’t help ^ Silly comment ^

I have one for each team, so its all team dependent. Also only one will be called at a time per client.

Man that’s some sloppy ass code.
Anyways, instead of using:



if spawn == 1 then


Do this:



if ply:Team() == 1 then


You should really use util.AddNetworkString at the top of the file (or somewhere it’ll basically be called during the gamemode startup) as it only needs to be registered once. If you attempt to register it just before performing another net function, the following net message will in most cases silently fail.

What John552 also said is correct, you don’t need that many unique net messages given they all do the exact same thing. That isn’t the cause of your issue, but it is a sanitary issue.

I have also tried that and also calling upon the teams name which is declared within the shared.lua file.
I know that the method being used to receive works as I am using other methods within the player spawn function with the same type of net message.

I don’t understand whats wrong with this it should work…

This may not have anything do do with this but:



ply:SetTeam( spawn[math.random(1,#spawn)] )--Set the team to contenders.


Why?

Using this is more efficient:



ply:SetTeam( table.Random(spawn) )


Actually his is more efficient if you are using an array (a table with sequencing numerical keys).

You are only using 1, 11, 12, 13, 14, 15 as possible teams, yet math.random( 1, #spawn ) will always be a number between 1 and 6, meaning

1) you are using team numbers that you presumeably shouldn’t/don’t want to be using (2, 3, 4, 5, and 6) and

2) 11, 12, 13, 14, and 15 are never going to be chosen

E.g.,


] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
4
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
1
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
3
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
5
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
6
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
6
] lua_run_cl local spawn = { 1, 11, 12, 13, 14, 15 } print( math.random( 1, #spawn ) )
4

You should use** table.Random( { 1, 11, 12, 13, 14, 15 } )** instead, which will guarantee to always be a value within that table.

Also, maybe I am entirely missing a line of code which sets spawn to a number, but all I see you doing is trying to equate a table with a number.


local spawn = {1,11,12,13,14,15}

..

if spawn == 1 then --Contender 

Is just doing


if {1,11,12,13,14,15} == 1

which is never going to be true.

Am I just missing a line somewhere in your code? It’s rather hard to read with the mismatched indenting.

I will explain a lot clearer what I am trying to do and perhaps you guys will find it easier to help me.

A player spawns on a random team, the numerical options are due to other teams which I don’t want to be chosen.

When you spawn on the team I want to send a net message to the client. So if you are on team 1 send this net message but if your team 11 send this one and so on so forth.

It doesn’t matter regarding the amount of net messages as I had this working a different way but it was horrendous tbh. Here is what I was using before perhaps you could help me by rewriting or pointing out something with it.




if ply:Team() ~= Contenders and math.random(1,1) == 1 then --Contender
	ply:SetTeam( 1 ) 	
 util.AddNetworkString( "contender" )
	 net.Start("contender")
     net.WriteString("some text")
     net.Send(ply)	
	 end
if ply:Team() ~= Contenders and math.random(1,5) == 1 then --Mr.sLayer
	ply:SetTeam( 11 )
	 util.AddNetworkString( "slayer" )
	 net.Start("slayer")
     net.WriteString("some text")
     net.Send(ply)	
	end
if ply:Team() ~= Contenders and math.random(1,5) == 2 then --Hunter
	ply:SetTeam( 12 )
	 util.AddNetworkString( "hunter" )
	 net.Start("hunter")
     net.WriteString("some text")
     net.Send(ply)	
	end
if ply:Team() ~= Contenders and math.random(1,5) == 3 then --Liberator
	ply:SetTeam( 13 )
	 util.AddNetworkString( "lib" )
	 net.Start("lib")
     net.WriteString("some text")
     net.Send(ply)	
	end
if ply:Team() ~= Contenders and math.random(1,5) == 4 then --Abominator
	ply:SetTeam( 14 )
	 util.AddNetworkString( "abominator" )
	 net.Start("abominator")
     net.WriteString("some text")
     net.Send(ply)	
	end
 if ply:Team() ~= Contenders and math.random(1,5) == 5 then --Deadman
	ply:SetTeam( 15 )
	 util.AddNetworkString( "deadman" )
	 net.Start("deadman")
     net.WriteString("some text")
     net.Send(ply)	
	end



Thanks

Duby

We’ve already pointed out the multiple issues with your current code.

  • Don’t use unnecessary net messages that a single message can handle.
  • Don’t use math.random( 1, #spawn ) the way you are doing it because it uses indexes 1 through 6, not 1, 11 through 15.
  • Don’t compare a table (spawn) with a number.

If you’re really still needing spoon feeding, try simplifying things like this (untested, of course):



-- put somewhere at the top of your file (if serverside only) or put it inside the if ( SERVER ) block if shared
util.AddNetworkString( "SetTeam" )
util.AddNetworkString( "spawn2" ) -- get a better naming convention than this, it's very non-descriptive

-- define the teams to be constants instead of using magic numbers
-- you might've already defined these elsewhere, so use them
-- whatever you do, just stop using magic numbers
CONTENDER  = 1
SLAYER     = 11
HUNTER     = 12
LIBERATOR  = 13
ABOMINATOR = 14
DEADMAN    = 15

-- initializes a table to hold your teams, defined above
-- this is so you can correctly use math.random
local teams = {
    CONTENDER,
    SLAYER,
    HUNTER,
    LIBERATOR,
    ABOMINATOR,
    DEADMAN
}

-- initializes a table to hold the messages you want to send to players
-- this will be used as a lookup table so you don't have to use horrid if-else code blocks
local teamMessages = {
    [CONTENDER]  = "You are in the Contender team!",
    [SLAYER]     = "You are in the Slayer team!",
    [HUNTER]     = "You are in the Hunter team!",
    [LIBERATOR]  = "You are in the Liberator team!",
    [ABOMINATOR] = "You are in the Abominator team!",
    [DEADMAN]    = "You are in the Deadman team!"
}

.....
.....
.....

ply:SetTeam( math.random( 1, #teams ) )

-- whatever this is for
net.Start( "spawn2" ) 
    net.WriteString( "some text" )
net.Send( ply )

-- send a message informing them that they joined the team, or whatever
net.Start( "SetTeam" )
    net.WriteString( teamMessages[ ply:GetTeam() ] )
net.Send( ply )


You’d then change your previous mass of net messages for teams into just this:



net.Receive( "SetTeam", function( bits )
    local msg = net.ReadString()
    -- do whatever you want with it
end )


I see thanks. Also I am not being spoon fed thankyou very much, I have given out my code which needed to be corrected or changed. So its hardly getting someone to do it from scratch for me is it?

Thanks for the code I will try it out and see what I can do with it.

We gave you multiple suggestions that would fix your problem, yet you seemed to ignore attempting to fix it yourself with some of those suggestions. That is what I mean by spoon feeding you.

The main issue I had if you read the topic no disrespect that I couldn’t get the net messages working according to each team.

He is helping you, don’t be a dick to someone who has literally just given you a much better, more efficient and sleeker piece of code for absolutely no fee. The words you are looking for are “Thank you”

How am I not being thankful? I just said the message before last thanks, and also throughout any comments anyone has given me. I was only pointing something out. If you read every comment properly instead of skim reading it you wouldn’t of made that statement. :L

[editline]3rd July 2014[/editline]

This solved my issue. When I tried it out last night I was rather tired and must of made a typo error or something. I also used the random team selection which one of you suggested. Thanks for the quick replies and patience.

-Duby