• What am I doing wrong here? Trying to do a health check.
    17 replies, posted
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? [lua] print("Healthcheck script has been ran.") if player.Health == 100 then Msg("You are feeling perfectly fine.") else Msg("You are hurt.") end [/lua] 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
[QUOTE=Deadman123;33435042]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[/QUOTE] 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? ---EDIT: 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
[QUOTE=Deadman123;33435459]Are you trying to do this at an interval? I can help if I know this[/QUOTE] 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?
[QUOTE=leiftiger;33435624]Where do you define player? Is this the full code?[/QUOTE] I didn't know I needed to :/ 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") else Msg("You Are Hurt end end[/LUA]
[QUOTE=Deadman123;33435641][QUOTE 1337Narb]Also how would I get it so it prints to the point of you being able to see the message in the chat window? [/QUOTE][/QUOTE] I mean like the window in the bottom left corner of your screen.
Very simple lua_openscript_cl concept ([i]if you don't want it to run at intervals[/i]): [lua] MsgN("Check health script running") local player = LocalPlayer() if player:Health() == 100 then chat.AddText("You are feeling perfectly fine.") else chat.AddText("You are hurt.") end [/lua]
@1337Narb :suicide:
[QUOTE=1337Narb;33435663][/QUOTE] I mean like the window in the bottom left corner of your screen.[/QUOTE] It's called a hud, [url]http://wiki.garrysmod.com/?title=Simple_HUD_Tut[/url]
[QUOTE=leiftiger;33435669]Very simple lua_openscript_cl concept ([i]if you don't want it to run at intervals[/i]): [lua] MsgN("Check health script running") local player = LocalPlayer() if player:Health() == 100 then chat.AddText("You are feeling perfectly fine.") else chat.AddText("You are hurt.") end [/lua][/QUOTE] Thank you so much <3
where did you get that function Health() ? Where can I see them all?
[url]http://wiki.garrysmod.com[/url] It's all documented there.
[url]http://wiki.garrysmod.com/?title=Player[/url] [url]http://wiki.garrysmod.com/?title=Entity[/url] [editline]27th November 2011[/editline] Ninja'd
[QUOTE=_NewBee;33460758][url]http://wiki.garrysmod.com/?title=Player[/url] [url]http://wiki.garrysmod.com/?title=Entity[/url] [editline]27th November 2011[/editline] Ninja'd[/QUOTE] Thanks. How did You find it? In what section?
Never use player as a variable name. You'll start overwriting the player library.
Sorry, you need to Log In to post a reply to this thread.