I tried made the script, that kicks player, when he damages NPC or NPC health < 100%, but it’s doesn’t work.
local man = script.Parent
local humanoid = man.Humanoid
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(Player)
if humanoid.Health < 100 then
Player:kick("Don't shoot at allies.")
end)
if you would like to make a script that fires a block of code when the npc’s health decreases, then:
local humanoid = script.Parent.Humanoid
local lasthealth = nil
function healthdecreased(newhp, oldhp)
print("Don't shoot at allies")
end
humanoid.HealthChanged:Connect(function(health)
if lasthealth then
if lasthealth == health then -maxhealth changed
else --health changed
if lasthealth > health then --health decreased
healthdecreased(health, lasthealth)
end
end
end
lasthealth = health
end)
NOTE: it does not kick anybody, because you would need another system for shot detection most likely using raycasting.