How could I check if a NPC is added?

Hey! So I’m using a script that makes blood drops when a player is damaged. The script is a server script so everything is controlled by one script. I want to use this same effect for the enemies in my game and keep using only one script. Is there a way to check if a Humanoid or a NPC has been added to the workspace or a folder. Something like when using PlayerAdded

I tried looking around for a solution but couldn’t find something that was similar enough to my issue.

(I didn’t make this script I used a YouTube tutorial)

local HealthPerDrop = 7
local DropLifeTime = 3.5
local DropSizeMin, DropSizeMax = 0.1, 2.4

local Blood = script:WaitForChild("Blood")

local tweenservice = game:GetService("TweenService")
local tweeninfo = TweenInfo.new(0.3, Enum.EasingStyle.Quint, Enum.EasingDirection.InOut)



game.Players.PlayerAdded:Connect(function(plr)
	
	plr.CharacterAdded:Connect(function(char)
		
		local hum = char:WaitForChild("Humanoid")
		
		local PreviousHealth = hum.Health
		
		hum.HealthChanged:Connect(function(NewHealth)
			
			local HealthDif = PreviousHealth - NewHealth
			
			PreviousHealth = NewHealth
			
			if HealthDif < 0 then return end
			
			local NumOfDrops = math.floor(HealthDif/ HealthPerDrop)
			
			local DropsFolder = Instance.new("Folder", workspace)
			
			for i = 1, NumOfDrops do
				
				local DropSize = math.random(DropSizeMin * 10, DropSizeMax * 10) / 10
				
				local BloodDrop = Blood:Clone()
				BloodDrop.Size = Vector3.new(0,0,0)
				
				local xPos = char.HumanoidRootPart.Position.X + math.random(-30,30) / 10
				local yPos = char.HumanoidRootPart.Position.Y - char:WaitForChild("Left Leg").Size.Y/2 + 0.05
				local zPos = char.HumanoidRootPart.Position.Z + math.random(-30,30) / 10
				
				BloodDrop.Position = Vector3.new(xPos, yPos, zPos)
				
				BloodDrop.Parent = DropsFolder
				
				tweenservice:Create(BloodDrop, tweeninfo, {Size = Vector3.new(0.05, DropSize, DropSize)}):Play()
				
			end
			
			wait(DropLifeTime)
			DropsFolder:Destroy()
		end)
		
	end)
	
end)

You should be using ChildAdded which would get the Item that has been add into the Instance

I think you can do something like:

while true do
    wait(0.1)
   game:GetService(“Workspace”):WaitForChild(“NPCName”)
end

So i could do something like

Local zombie = Folder.ChildAdded

If zombie.name = Zombie then

Do all the stuff and things

End

ChildAdded is a Connection that has the Instance that was Added, so yes:

SomeInstance.ChildAdded:connect(function(Object)
    if Object.Name = "Zombie" then
        -- code
    end
end)
1 Like

Ah gotcha. Im currently not home so I cant test it out but thanks for the help! Have a good day

1 Like

I dont think you understand how WaitForChild works.

Wait, wouldn’t my script wait for the child to be added? That’s what WaitForChild is for

no, it would be a waste of resources to do so, Instance.ChildAdded would be much more efficient for this usage, plus WaitForChild has a Time out, so if it doesn’t find the object within a period of time, it will yield, so your your code is basically this:

while true do -- creates a loop
    wait(.1) -- waits .1 of a second

    workspace:WaitForChild("NPCName") -- Yields until Child is found within a time limit
end

Do you mean this?

repeat wait() until workspace:FindFirstChild("NPCName", true)

Oh sorry, I’m not a Programmer, and yes I meant something like what you said, sorry for getting that wrong, I’m gonna remember for next time tho!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.