Jump to content

[lua] attempt to index error


dennis
 Share

Recommended Posts

Hey all,

 

I'm following project saturn tutorial series and now there is this problem with creating the inventory.

 

function Script:Use(player)
local playerInventory = player.script:GetInventory()

if playerInventory == nil then
error("No inventory")
end
System:Print("We got an inventory")
if not playerInventory:IsFull() then
System:Print("We place the bottle inside the inventory")
end
end

 

it's a real pain to find out what the problem is because the error I'm getting is:

"C:/Users/Dennis/Documents/Leadwerks/Projects/..." : 3 : attempt to index local 'player' (a nil value)

 

But I already declared the player inside Use right?

 

Can somebody please help?

 

Cheers

Link to comment
Share on other sites

It's saying that the value of the player variable is nil. So yes while you declared it as a parameter to the function, whatever calls this function must pass in a valid (non nil) value to the player variable. It would seem it's not. So look at whatever calls this function and see what it's passing into it.

Link to comment
Share on other sites

No I mean what calls your Script:Use() function that is passing in the player value to it's parameter variable?

 

It doesn't even get to call the GetInventory() function because your player variable is nil. That's what the error is telling you. So you have to look at why your player variable is nil. You declare the player variable as a function parameter to the Use() function. So whatever is calling that Use() function should be passing something in, but whatever it's passing in is nil or it's not passing anything in which the default value is then set to nil.

Link to comment
Share on other sites

I have seen this happening before. If you are following the entire tutorial serie, then at one point you changed the following line in the fpsplayer.lua to this. (around line 290)

--Use the object, whatever it may be
usableentity.script:Use(self.entity)

 

The reason why it probably no longer works:

When Leadwerks has an official update that changes the FPSplayer.lua, it replaces your lua file with the new one. That means that all your changes so far, are now gone (You can access the backup which is in the same folder). If an update has been pushed by Leadwerks that changes the script (somewhere last month the player script has been updated), then the line looks as follows:

--Use the object, whatever it may be
usableentity.script:Use()

 

 

It is a quite annoying so I am going to switch to a separate FPSplayer script in order to prevent this problem from happening again.

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...