-
What do you want to achieve? Keep it simple and clear!
Essentially I’m trying to make a tycoon game where a player’s NPC targets other NPCs which are grouped in a folder from a spawn. The NPC targets the enemy NPCs but doesn’t do any damage. -
What is the issue? Include screenshots / videos if possible!
Script:
local npc = script.Parent
local animation = script:WaitForChild('SwordAnimation')
local humanoid = script.Parent:WaitForChild('Humanoid')
local animator = humanoid.Animator
local swordAnimation = animator:LoadAnimation(animation)
local sword = npc.Sword
local damagePart = sword.DamagePart
local damage = 10
--local function onTouch(otherPart) -- otherPart = Humanoid
-- local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
-- if humanoid then
-- humanoid -= damage
-- end
--end
local function onTouch() -- otherPart = Humanoid
local enemyNpcs = workspace.TestFolder:GetChildren()
if enemyNpcs.className == "Model" then
enemyNpcs:FindFirstChild("Humanoid"):TakeDamage(10)
end
end
function findNearestTorso(pos)
local list = workspace.TestFolder:GetChildren()
local torso = nil
local dist = 1000
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) then
temp = temp2:findFirstChild("HumanoidRootPart")
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
while true do
wait(math.random(1,5))
local target = findNearestTorso(script.Parent.HumanoidRootPart.Position)
if target ~= nil then
script.Parent.Humanoid:MoveTo(target.Position, target)
swordAnimation:Play()
if swordAnimation:Play() then
damagePart.Touched:Connect(onTouch)
end
end
end
-
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I have tried looking for solutions as well as video but was unable to find any or were not quite what I was looking for.