Setting a texture depending on what team a player is on

I’ve tried doing if statements to check if a player is on a certain team.

These are the ones i’ve tried:

First Try
[lua]if LocalPlayer():Team() == 1 then
HUDTex = surface.GetTextureID( “RedvsBlue/blue-health” )
elseif LocalPlayer():Team() == 2 then
HUDTex = surface.GetTextureID( “RedvsBlue/red-health” )
end

surface.SetTexture(HUDTex)
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )[/lua]

Second Try
[lua]if LocalPlayer():Team() == 1 then
HUDTex = surface.GetTextureID( “RedvsBlue/blue-health” )
surface.SetTexture(HUDTex)
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
elseif LocalPlayer():Team() == 2 then
HUDTex = surface.GetTextureID( “RedvsBlue/red-health” )
surface.SetTexture(HUDTex)
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
end[/lua]

Third Try
[lua]HUDTexRed = surface.GetTextureID( “RedvsBlue/red-health” )
HUDTexBlue = surface.GetTextureID( “RedvsBlue/blue-health” )

if LocalPlayer():Team() == 1 then
surface.SetTexture(HUDTexBlue)
elseif LocalPlayer():Team() == 2 then
surface.SetTexture(HUDTexRed)
end

surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )[/lua]

Fourth Try
[lua]HUDTexRed = surface.GetTextureID( “RedvsBlue/red-health” )
HUDTexBlue = surface.GetTextureID( “RedvsBlue/blue-health” )

if LocalPlayer():Team() == 1 then
surface.SetTexture(HUDTexBlue)
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
elseif LocalPlayer():Team() == 2 then
surface.SetTexture(HUDTexRed)
surface.SetDrawColor( 255, 255, 255, 255 )
surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
end
[/lua]

None of the above worked, any help figuring this out would be appreciated.

These are ALL called within a function hooked to **[Gamemode.HUDPaint

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.HUDPaint)**

This is what it SHOULD look like: (Please note, this is NOT using an if statement, and just setting it to use the Red texture)

http://img299.imageshack.us/img299/3079/ingamew.png

It is JUST the part where the health and armour are, the scoring at the top is not to do with this.

Where are you putting it? I hope in HUDPaint right?

Yes, I edited the OP to say that, and as you can see from the screenshot, the scoring texture works fine.

That looks quite nice.

Anyways, are you sure that the team integers are correct? That they’re actually 1 and 2?

Yes, because I set the models depending on the teams, and they seem to work.

Debug by printing LocalPlayer():Team(). Tells us what it outputs.

[img_thumb]http://img12.imageshack.us/img12/7074/redmdl.png[/img_thumb] [img_thumb]http://img291.imageshack.us/img291/3908/bluemdl.png[/img_thumb]

Click each image to enlarge, it’s proof the models work.

Also, will do nonsense, hang on a sec.

EDIT:

Okay nonsense, I printed LocalPlayer():Team() and it prints either 1 or 2 depending on what team I chose to begin with. In this case, it printed 1:

http://img713.imageshack.us/img713/1272/debug.png

Recheck the texture names to make sure they use dashes and not underscores? Might be one of those random things…

I didn’t say they wouldn’t draw, I said I can’t get them to work in an if statement, they work on their own, but do not draw if i put them inside an if statement.

bump, need an answer still.

First of all… Dont bump right after 4 hours.

Second, the method you are using to get the player is not correct. Being a team based gamemode that you are working on, it would require Multiplayer, which pretty much fucks over your LocalPlayer().

Instead why dont you try moving this code to the GM:PlayerSpawn(pl) section and change your LocalPlayer() to “pl” like this:



function GM:PlayerSpawn(pl)
     HUDTexRed = surface.GetTextureID( "RedvsBlue/red-health" )
     HUDTexBlue = surface.GetTextureID( "RedvsBlue/blue-health" )
 
     if pl:Team() == 1 then
         surface.SetTexture(HUDTexBlue)
         surface.SetDrawColor( 255, 255, 255, 255 )
         surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
     elseif pl:Team() == 2 then
         surface.SetTexture(HUDTexRed)
         surface.SetDrawColor( 255, 255, 255, 255 )
         surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
     end
end


Also. For future reference. When something doesnt work… Debug the FUCK out of it like so:



function GM:PlayerSpawn(pl)
     HUDTexRed = surface.GetTextureID( "RedvsBlue/red-health" )
     HUDTexBlue = surface.GetTextureID( "RedvsBlue/blue-health" )
     print("Starting to pick team.")
     if pl:Team() == 1 then
         print("Team is 1, Texture should be blue")
         surface.SetTexture(HUDTexBlue)
         surface.SetDrawColor( 255, 255, 255, 255 )
         surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
     elseif pl:Team() == 2 then
         print("Team is 2, Texture should be red")
         surface.SetTexture(HUDTexRed)
         surface.SetDrawColor( 255, 255, 255, 255 )
         surface.DrawTexturedRect( 0, ScrH() - 100, 300, 100 )
     end
     print("Team Check finished. Texture should be applied.")
end


Edit:
TOTALLY misunderstood what the issue/goal was. Keeping info for you to read though.
Heh. thought you were trying to set the texture of the player model or something. XD

First of all, the hook PlayerSpawn is serverside, and surface functions are clientside, therefore not possible to be in the same function like that.

Yeah. I didn’t edit it in time. I completely misunderstood the goal of your script.

Point still stands though. Debug your code and see whats going on.