Hey everyone, I ran into a snag with one of my scripts that I could use some advice on. I’ve created a door script that detects an NPC’s health and once that NPC’s health reaches a certain threshhold it opens up. This script works fine when the NPC is placed in the workspace before testing but when the NPC is spawned in from ServerStorage the script no longer functions. How should I go about getting the script to Detect an NPC’s health that is spawned in during play from ServerStorage? I’ve also included the script in question below.
Summary
while true do
wait (0.01)
local h = game.Workspace["Kobold Shaman"]:findFirstChild("Monster")
if h~=nil then
if h.Health <=0 then
script.Parent.CanCollide = false
script.Parent.Transparency = 0.75
wait(10)
script.Parent.CanCollide = true
script.Parent.Transparency = 0
end
end
end
here is some demo code so you can see how its done
game.Workspace["Kobold Shaman"].ChildAdded:Connect(function(child)
-- if the child added is not Monster then exit you may not need this if all children will be monsters
-- if child.Name ~= "Monster" then return end
child.Humanoid.HealthChanged:Connect(function(health)
-- if the health is more then 0 do nothing and exit
if health > 0 then return end
-- open door
script.Parent.CanCollide = false
script.Parent.Transparency = 0.75
-- wait 10 seconds
task.wait(10)
-- close the door
script.Parent.CanCollide = true
script.Parent.Transparency = 0
end)
end)
Hey thanks for the reply 5uphi, I was playing around with your demo code to trying to learn how best to approach this and compare it to how i’ve drafted up the script. But the demo code you’ve linked doesnt seem to work, whether the NPC is spawned in from ServerStorage or placed in the workspace the door never actually alows the player to pass through. And after taking a closer look at the Humanoid.HealthChanged page on the Developer page I noticed that Humanoid.Health scripts apparently only function as local scripts which is most likely the root cause of the test code not working. Is there possibly another solution?
When the player attacks the enemy are you changing the health in a localscript or normal script
If your changing the health in a localscript then only the local player will detect the health changing and the health will not be replicated to the server and other players