Jump to content
dennis

[LUA] Get any entity in world with "health" ?

Recommended Posts

Hi all,

 

I was wondering how I can get any entity in the world with a certain variable inside such as "health"?

This is because I am making a script with exploding stuff in it. but when something near it with the health variable, it has to get the same amount of damage too.

 

Thanks in common.

Share this post


Link to post

I'd use GetEntityNeighbors() function in Scripts/Functions/GetEntityNeighbors.lua.

 

You could do something like:

local radius = 10
local entities = GetEntityNeighbors(self.entity, radius, true)

for e=1, #entities do
 if entities[e].script.health ~= nil then
   --deal damage
 end
end
Edited by thehankinator

Share this post


Link to post

I'd use GetEntityNeighbors() function in Scripts/Functions/GetEntityNeighbors.lua.

 

You could do something like:

local radius = 10
local entities = GetEntityNeighbors(self.entity, radius, true)

for e=0, #entities do
if entities[e].script.health ~= nil then
--deal damage
end
end

 

Won't work for me, it gave me: 119 : attempt to index a nil value.

Share this post


Link to post

Won't work for me, it gave me: 119 : attempt to index a nil value.

Did you import the GetEntityNeighbors script into your script before trying to use the above code?

Share this post


Link to post

Did you import the GetEntityNeighbors script into your script before trying to use the above code?

Yes, I have.

It gave me the error on:

 if entities[e].script.health ~= nil then

Share this post


Link to post

As a guess, maybe check the entity has a script exists before you access something on it?

 

Some entities coming back are probably not the ones you want to look at and may not have scripts.

Share this post


Link to post

As a guess, maybe check the entity has a script exists before you access something on it?

 

Some entities coming back are probably not the ones you want to look at and may not have scripts.

Entities next to it (between the radius of 10) are my character and another object with that value. I am not getting why it wouldn't work.

I can't really return the values using System:Print() because we can't print tables to the system. So I don't know what is returned

 

I tried another value in the same script, but it is still a Nil value.

So I guess the:

 

for e=0, #entities do
end

isn't working.

Share this post


Link to post

It should be:

for e=1, #entities do

 

end

 

Macklebee ! you're the hero of today :D

Completely works.

Share this post


Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

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.

×
×
  • Create New...