Repeat loop help

I need to print the weapon name on the screen for 2.5 seconds when the player changes weapons. I looked for help on the Garrysmod Wiki and found that using a repeat loop and os.clock would work best.

Heres what I have so far:

[lua]
function ShowWeaponName ()
local starttime = os.clock()
local old_wep = LocalPlayer():GetActiveWeapon()

if tonumber(old_wep) != tonumber(LocalPlayer():GetActiveWeapon()) then
	repeat
		draw.SimpleText( LocalPlayer():GetActiveWeapon():GetPrintName(), "BANKGOTHICSM", ScrW() - 220, ScrH() - 120, Color( 255, 255, 255, 255 ), TEXT_ALIGN_CENTER, TEXT_ALIGN_CENTER )
	until os.clock() - starttime > 2
end

end
hook.Add( “HUDPaint”, “DrawWeaponName”, ShowWeaponName)
[/lua]

What is wrong? I took the example from the website but this isn’t working… Maybe the weapon check is wrong? I know the text position and everything on that line works.

HUDPaint is a hook called every frame. Using a repeat/until in HUDPaint would simply draw a lot of something in every frame, not over time. If you want to fade over time, use **[G.CurTime

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=G.CurTime)** and make a timer.

I didn’t even know repeat loops existed in gLua. Oh well, learn something new every day :eng101:

Could you just use a client-based variable and set it to CurTime() + 2.5?

Isn’t this wrong?

[lua]
if tonumber(old_wep) != tonumber(LocalPlayer():GetActiveWeapon()) then
[/lua]

Shouldn’t it be this?

[lua]
if ! tonumber(old_wep) == tonumber(LocalPlayer():GetActiveWeapon()) then
[/lua]

I’m a noob to Lua though and I am probably wrong :slight_smile:

The second one is even more wrong that the first one. Proper Lua syntax would be:

[lua]
if old_wep ~= LocalPlayer():GetActiveWeapon() then
[/lua]

Also, why tonumber? You can compare two values even if they aren’t numbers.

I used tonumber because I thought it would be easier to compare. I don’t really know that is is returning but I figured if it returned a number it could be compared. If you say I don’t need it then I will leave it out. Thanks.

It won’t be easier to compare, actually it will screw your stuff up. tonumber is used on strings to turn them into numbers (for instance, “123” becomes 123), this is sometimes necessary because Lua doesn’t always automatically convert a string containing a numeric value into an actual number. Use it on an invalid string or anything else than a string, and it will return nil (which pretty much means “no value”).

So basically, if you use tonumber on each entity and compare them, it will always be equivalent to comparing nil with nil. And Lua will think your condition is always true, even though the entities are different.