Jump to content

attempt to index field 'player' (a nil value)


CoolBeans
 Share

Recommended Posts

I hate to start a topic for this but just about every tutorial I do comes up with it from Jorns

Health bar tutorial to Blue Hornets awesome Hud tutorial to others as well.

Is my FPSplayer script goofed or something? Even when starting a new project with new

FPSplayer script same error. Odd thing is sometimes I get Jorns health bar to work.

I tripled checked the code so there should be few errors now.

 

Ive added the hud code - line 38 error please help.

l also added the FPSplayer script just in case.

 

**********************************************************************

 

I just created a new project. Made a box. Dragged FPSplayer prefab to scene.

Added Pivot. Added script helathbar.lua back up direct from Jorns download.

Error line 27 as mentioned. ARRRRRRRRRRRR included in download.

 

line 27

local healthFactor = self.player.script.health / self.player.script.maxHealth

 

Thanks a lot

CB

Link to comment
Share on other sites

I knew it had to be something simple but vastly missed. Thanks a ton it got me further but with trouble still.

That also explains the intermittent working. Problem now is if I try the healthbar from Jorns on its own it works

but when I fire a weapon the health bar changes yellow. Sometimes I can hold the fire down and keep it

yellow. Must have something to do with Red for background and green for overlay mixed with muzzle blast problem?

 

As for the Hud I have a new error and checked the code on the line I think it's ok?

 

Attempt to index a nil value line 40

 

self.ClipAmmoText = "Round in Mag = " .. self.player.script.weapons[self.player.script.currentweaponindex].clipammo

 

Thanks again.

 

CB

 

 

 

Edit:

 

Hmmm I added the prefab weapon to the player and got passed that error to a new one.

I hope I dont have to have a weapon in inv already for it to work. New error I get after adding

prefab gun is.

 

attempt to concatenate global 'killScore' (a nil value

line 42

 

self.ScoreText = "Score = " .. KillScore

 

Assuming I'm missing something again, obviously. Looking into it wink.png

 

CB

Link to comment
Share on other sites

attempt to concatenate global 'killScore' (a nil value

line 42

 

self.ScoreText = "Score = " .. KillScore

 

CB

 

KillScore has nothing assigned to it. Maybe define it KillScore=0 at the top of App.lua if you want to keep it global.

---

Scott

 

Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060

Link to comment
Share on other sites

Thanks for the fix on the health bar flashing from the other topic.

http://www.leadwerks.com/werkspace/topic/10817-problem-with-pistol-prefab/

 

Well maybe I'm in too deep on this one. This is the HUD tutorial from Blue Hornet.

I'm starting to think he did some prior tutorials and there is some code I'm missing in other areas like FPSplayer for example.

Besides the one already done.

 

Killscore for example must be else where. I need to learn the global, local, place in lua.app or not ect have not gone over a good description of how the order of things work in lua perhaps I shall.

 

I added the killscore = 0 at the top of lua got me passed that error and on to the next ;(

 

error in function 'DrawImage'.

argument #6 is '[no object]';'number' expected.

line 66

 

 

context:DrawImage(self.HudRightImage,context:GetWidth()-self.HudRightImage:GetWidth(),context:GetHeight()-self.HudRightImage:GetWidth(),context:GetHeight())

 

Thank you,

CB

 

Edit: another problem I'm having with a lot of tutorials is the FPSplayer is not the same code in most examples.

even on tutorials that are only 2 months old.

Link to comment
Share on other sites

There is an extra parameter entered which you don't need. smile.png

 

Change this:

context:DrawImage(self.HudRightImage,context:GetWidth()-self.HudRightImage:GetWidth(),context:GetHeight()-self.HudRightImage:GetWidth(),context:GetWidth(),context:GetHeight())

 

to this:

context:DrawImage(self.HudRightImage, context:GetWidth()-self.HudRightImage:GetWidth(), context:GetHeight()-self.HudRightImage:GetHeight())

 

Context::DrawImage

 

Syntax

  • void DrawImage(Texture* texture, int x, int y, int width, int height)
  • void DrawImage(Texture* texture, int x, int y)

---

Scott

 

Using Windows 7 Ultimate 64 bit/Core I7-2700K @ 4312mhz/24G RAM/Nvidia GTX 1060

Link to comment
Share on other sites

Odd it was there, copied it from his tutorial wink.png Again thanks so much you have helped me advance more in 24 hours then I have in 2 weeks. I got some oddities I'm going to try to tune on myself with the display but the ammo count works as well as clip and health.This is awesome! 2 Problems I got to ask at this point since you are so kind to help. Do you know how I can start with no weapon and not get the nil error? In my game you start with no guns. The other problem is the prefab machete doesn't work if added to a game with the Hud script active. I get the hand icon and I can manually pick it up. Swept over auto pickup not working as well as can't equip or use it.I added shot gun working fine. Something to do with the machete not having ammo mechanics .. and the Hud script?

 

I have awesome in game sound, day night cycle and now the hud sorta working as well as navmash and ai, this is monumental ;) Can anyone tell me how to make screen shots in game? It doesn't capture the game app.

 

CB

Link to comment
Share on other sites

The KillScore variable is made in Part 2 of the HUD tutorial. It isn't done as clear as it could be but it is a global value.

 

To fix the nil error when you have no guns you have to do a check around the AmmoText portion in UpdateWorld(). you want it to check if the player has a weapon and if he does then do the text like the tutorial and if not then make a blank text. If i get time later i will post a video to fix that issue.

 

There are three basic scopes of variables. there are global and local. there are two different scopes to local variables. that confused me for a while.

Global variables are accessible and modifiable in all scripts in the program. Global variables are made by adding them to the top of the App.lua file. and you do not have to say Script.variableName = whateverValue you can just say variableName = whateverValue It is a good practice to avoid global variables as they can become confusing and lost in the shuffle.

 

Local variables come in two scopes. The easiest way to explain this is probably one is local to a script and one is local to a function.

A local variable to a script is the ones that are at the top of a script and have the Script.variableName = whateverValue they are only in the scope of that script and are not usable outside of the script they are in without calling that script with the entity.script.variableName

A local variable to a function is they type you see the line "local variableName" and they are only in the scope of the function they are in and are not accessable outside that function unless they are passed through the return of the function.

 

This is probably alot of confusing due to my poor explanation. I am planning on doing a basic Lua tutorial to explain alot of the things done in Lua and how they work. I have been reading Programming in Lua (PiL) a book written by the architect of Lua script. I have learned alot.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...