ply:SetColor() Not working?

I tried using the function above for my game, however it isn’t working. Can someone please help me figure out what’s wrong?

Show us your code.



function GM:PlayerLoadout(ply)

	ply:Give("fists_weapon")
	ply:SetModel("models/player/gman_high.mdl")
	
	if(ply:Team()==1) then
		ply:SetColor(0,0,0,255)
	end
	if(ply:Team()==2) then
		ply:SetColor(0,255,0,255)
	end
	if(ply:Team()==3) then
		ply:SetColor(255,0,0,255)
	end
	if(ply:Team()==4) then
		ply:SetColor(238,0,238,255)
	end

end



Which gamemode are you trying to do this for?

does it write any error message?

-snip-

SetColor has never worked properly on players as far as I know. Last time I tried, doing ply:SetRenderMode(RENDERMODE_TRANSCOLOR) before setting the color works. Not sure it still works now.

If it doesn’t, there is this way, which is a bit more tricky. It’s also fully clientside, don’t go and put this serverside and wonder why it doesn’t work.

[lua]local teamcolors = {
{0,0,0},
{0,1,0},
{1,0,0},
{0.93,0,0.93},
}

hook.Add(“PrePlayerDraw”, “PrePlayerColorOverride”, function(ply)
local c = teamcolors[ply:Team()]
if c then
render.SetColorModulation(unpack©)
end
end)

hook.Add(“PostPlayerDraw”, “PostPlayerColorOverride”, function(ply)
render.SetColorModulation(1,1,1)
end)[/lua]

render.SetColorModulation changes the color of everything that’s drawn after it is called. As you can see it takes RGB values between 0 and 1, and not 0 and 255, be aware of that.
If you want transparency as well, take a look at render.SetBlend, it works the same way except you give it only the alpha value between 0 and 1.
PrePlayerDraw is a hook which is called before a player is drawn, PostPlayerDraw is called after, so before a player is drawn, you check which team they are on and change their color accordingly, and once the player is done drawing, you revert the settings back to normal (1,1,1 is white).

Just in case you don’t know, “if c then” verifies that c isn’t nil, which happens when you try to get a value from a table at an empty location. For instance, players who belong to team 5 will not have their color altered, because there is no 5th element in teamcolors.
The unpack function turns a list into a sequence of values. Doing something like “hello(unpack({a, b, c}))” would be the same as doing “hello(a, b, c)”. That’s very convenient in some situations such as this one, where you don’t want to bother copying the render.SetColorModulation part 4 times with different values.

Useful links:
**[Render.SetColorModulation

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Render.SetColorModulation)**
**[Render.SetBlend

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Render.SetBlend)**
**[Gamemode.PrePlayerDraw

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

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

Hope that helped. :eng101:

[editline]17th March 2011[/editline]

Actually, here’s a more general solution, I’m not sure it works though, but I see no reason for it not to work.

[lua]
hook.Add(“PrePlayerDraw”, “PrePlayerColorOverride”, function(ply)
local r, g, b, a = ply:GetColor()

if r<255 or g<255 or b<255 then
	render.SetColorModulation(r/255, g/255, b/255)
end

if a<255 then
	render.SetBlend(a/255)
end

end)

hook.Add(“PostPlayerDraw”, “PostPlayerColorOverride”, function(ply)
render.SetColorModulation(1,1,1)
render.SetBlend(1)
end)
[/lua]

That would basically fix SetColor not working on players. Just set the color serverside and it should work.

I thought SetColor worked, it was just clientsided.

ply:SetColor( Color ) definitely works server-side as I use it in a gamemode I code.

[lua]
local c = team.GetColor(t[1])
ply:SetColor(c.r,c.g,c.b,c.a)[/lua]

It’s shared but only visible to you if you use it client-side.