Kick doesn't work, when trying to kill NPC

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)

this script is inside the NPC.

Just do

humanoid:GetPropertyChangedSignal(“Health”):Connect(function()

end)

this is because you are not listening for when it changes health, rather when a player joins

see Humanoid | Documentation - Roblox Creator Hub

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.

3 Likes

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