[Help] Setting all NPCs on Spawned to a certain Health
7 replies, posted
Hello,
I've been trying to get all Entities/NPCs to set their health on a slider, I've been looking for a while and asking around.
Im not sure how I could get all Entities or more complicated, making a square area box and setting the health in that Area.
Any help would be appreciated, Thank you
To set Health for all entities of certain classes you can use ents.FindByClass. For specific area you use ents.FindInBox but you need to visualize the are you select for convenience.
Heres the code I have so far,
I cant target NPC's
to change their health
Heres the code so far
TOOL.Category = "Test"
TOOL.Name = "NPC Area Health"
TOOL.Command = nil
TOOL.ConfigName = ""
language.Add("tool.npcareahealth.name","NPC Area Health")
language.Add("tool.npcareahealth.desc","Change a NPC's health")
language.Add("tool.npcareahealth.0","Left-Click to change the health of all NPC to the set value.")
TOOL.ClientConVar["health"] = "100"
function TOOL.BuildCPanel( panel )
panel:AddControl("Header", { Text = "Health", Description = "Left-Click to change the health of a NPC to the set value." })
panel:AddControl("Slider", { Label = "Health: ", min = 1,max = 5000, Command = "npcareahealth_health" })
end
function TOOL:LeftClick(tr)
if(tr.Entity:IsValid() && tr.Entity:IsNPC()) then
changeEntityHealth()
surface.PlaySound("buttons/button14.wav")
end
end
function changeEntityHealth()
for k, v in pairs( ents.FindByClass( "npc_*" ) ) do
v.Entity:SetHealth(self:GetClientNumber("health"))
end
end
Why your tool needs a npc on a player crosshair?
Why you need v.Entity when v is already your entity?
Alright I fixed it up abit, I still cant target NPCs or anything
TOOL.Category = "Test"
TOOL.Name = "NPC Area Health"
TOOL.Command = nil
TOOL.ConfigName = ""
language.Add("tool.npcareahealth.name","NPC Area Health")
language.Add("tool.npcareahealth.desc","Change a NPC's health")
language.Add("tool.npcareahealth.0","Left-Click to change the health of all NPC to the set value.")
TOOL.ClientConVar["health"] = "100"
function TOOL.BuildCPanel( panel )
panel:AddControl("Header", { Text = "Health", Description = "Left-Click to change the health of a NPC to the set value." })
panel:AddControl("Slider", { Label = "Health: ", min = 1,max = 5000, Command = "npcareahealth_health" })
end
function TOOL:LeftClick(tr)
--if(tr.Entity:IsValid() && tr.Entity:IsNPC()) then
changeEntityHealth()
surface.PlaySound("buttons/button14.wav")
--end
end
function changeEntityHealth()
for k, v in pairs( ents.FindByClass( "npc_*" ) ) do
v:SetHealth(self:GetClientNumber("health"))
end
end
Taking Entity out of v.Entity, screwed it up,
function changeEntityHealth()
for k, v in pairs( ents.FindByClass( "npc_*" ) ) do
v.Entity:SetHealth(self:GetClientNumber("health"))
end
end
Is the Code to change hp of every npc out there.
That code is a mess, when it's not getting a serverside error for calling clientside function it's getting one for trying to use a variable that was never declared.
Also it's not clear what you're trying to do, in the LeftClick function you're targetting a single NPC but then you use a function that targets all NPCs, (And can't because "self" only works within the tool function).
So let's go through all the errors:
First we have language.Add() being called serverside when it's only clientside stopping the server from reading the rest of the code.
To fix that you can add an if(CLIENT).
Then we have the surface.Playsound, whish is another clientside function being called serverside.
Just add another if(CLIENT) on that.
And lastly we have "self" being called on a function that doesn't have the context needed.
:GetClientNumber("health") can be called only on tools and "self" return the entity/variable that the function derives from.
Here's a few examples where "self" can be used: hastebin
Here's the fixed code: hastebin
That will only target the NPC you're looking at.
The reason why I made the server send the script to play the sound on the client is because on singleplayer (Which I assume is where you're testing this) the function TOOL/LeftClick can only be run serverside.
Now if you wanted to set the HP to all of them, I made a version for that too: hastebin
I kept the function you had before so you can see how it could work utilizing "self".
He wanted to set entities health in specified area so it shouldnt be:
TOOL.Category = "Test"
TOOL.Name = "NPC Area Health"
TOOL.Command = nil
TOOL.ConfigName = ""
if(CLIENT) then
language.Add("tool.npcareahealth.name","NPC Area Health")
language.Add("tool.npcareahealth.desc","Change a NPC's health")
language.Add("tool.npcareahealth.0","Left-Click to change the health of all NPC to the set value.")
end
TOOL.ClientConVar["health"] = "100"
function TOOL.BuildCPanel( panel )
panel:AddControl("Header", { Text = "Health", Description = "Left-Click to change the health of a NPC to the set value." })
panel:AddControl("Slider", { Label = "Health: ", min = 1,max = 5000, Command = "npcareahealth_health" })
end
function TOOL:LeftClick(tr)
local Target = tr.Entity
local Owner = self:GetOwner()
changeEntityHealth(self, tr)
Owner:SendLua("surface.PlaySound('buttons/button14.wav')")
end
function changeEntityHealth(tool, tr)
for k, v in pairs( ents.FindInSphere(tr.HitPos, 128 ) do -- for ex. radius set to 128 units
if(v:GetClass()=="npc_*")) then
v:SetHealth(tool:GetClientNumber("health"))
end
end
end
?
Sorry, you need to Log In to post a reply to this thread.