1. What do you want to achieve?
I’m trying to make the NPC only damage you whenever the right or left hand is touched.
2. What is the issue?
The NPC damages you even though it’s not anywhere near you.
3. What solutions have you tried so far?
I’ve looked at the script and couldn’t see what’s wrong. I’ve made sure it wasn’t the ability I was using that was damaging me.
Video: https://gyazo.com/01d97bd6ea275018d66bfffbea3775a0
Damage Script:
local lefthand = script.Parent.LeftHand
local char = script.Parent
local db = false
righthand.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and char.Humanoid.Health > 1 and hit.Parent ~= char and db == false and hit.Parent.Name ~= "Level 1" then
db = true
hit.Parent.Humanoid:TakeDamage(15)
wait(1)
db = false
end
end)
lefthand.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and char.Humanoid.Health > 1 and hit.Parent ~= char and db == false and hit.Parent.Name ~= "Level 1" then
db = true
hit.Parent.Humanoid:TakeDamage(15)
wait(1)
db = false
end
end)
Find a player script:
local oldHealth = 100
local pos = script.Parent.HumanoidRootPart.Position
function findNearestTorso(pos)
local list = game.Workspace:children()
local torso = nil
local dist = 25
local temp = nil
local human = nil
local temp2 = nil
for x = 1, #list do
temp2 = list[x]
if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2.Name ~= "Dummy") and (temp2:FindFirstChild("Humanoid")) and (temp2.Name ~= "Level 1") and (temp2.Humanoid.Health >= 1) then
temp = temp2:findFirstChild("UpperTorso")
human = temp2:findFirstChild("Humanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
end
end
end
end
return torso
end
hum.HealthChanged:Connect(function(newHealth)
if newHealth < oldHealth then
while true do
wait(0.5)
local target = findNearestTorso(script.Parent.UpperTorso.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
end
end
end
end)