What am I doing wrong here? Trying to do a health check.

Trying to learn lua, experimenting with things of my own. Probably making some stupid mistake here, but whatever.

Basically when the script is run; it’s meant to check if you’re hurt or not. If your health isn’t 100, then you are registered as hurt, and it prints this to the console.

Am I addressing the player’s health wrong?

print(“Healthcheck script has been ran.”)

if player.Health == 100 then
Msg(“You are feeling perfectly fine.”)
Msg(“You are hurt.”)

I always receive the message: “You are hurt!”, even when the player isn’t harmed at all.

I think this is because at line 3 you have [LUA]if player.health == 100 then[/LUA] when it should be [LUA]if (player:Health() == 100) then[/LUA] As health is a method, and with a . it would be a library if im correct

I have no idea on what a library is, or how to use one.

Also could you explain why the parenthesis are needed around player:Health?


Tried running your version of the script, got this:

[lua\anonymous\healthtest.lua:3] attempt to call method ‘Health’ (a nil value)

Are you trying to do this at an interval? I can help if I know this

Dunno, just trying to make it so when you run the lua file it checks if your health is something, then prints it to console.

Also how would I get it so it prints to the point of you being able to see the message in the chat window?

Where do you define player? Is this the full code?

I didn’t know I needed to :confused:

How would I go about doing this?
I’m new to lua, as I stated before.

You’d do it like this [LUA]function stuff(ply)
if (ply:Health() == 100) then
Msg(“You Feel Good”)
Msg("You Are Hurt

I mean like the window in the bottom left corner of your screen.

Very simple lua_openscript_cl concept (if you don’t want it to run at intervals):
MsgN(“Check health script running”)
local player = LocalPlayer()
if player:Health() == 100 then
chat.AddText(“You are feeling perfectly fine.”)
chat.AddText(“You are hurt.”)

@1337Narb :suicide:

I mean like the window in the bottom left corner of your screen.

It’s called a hud, http://wiki.garrysmod.com/?title=Simple_HUD_Tut

Thank you so much <3

where did you get that function Health() ?
Where can I see them all?

It’s all documented there.

[editline]27th November 2011[/editline]


Thanks. How did You find it? In what section?

Never use player as a variable name. You’ll start overwriting the player library.