I am making a simple npc, but the script isnt working. One more quick question, what would be the most lag reducing way to run this, beausce I am going to have a lot of them. Here is the script:
local enhum = script.Parent.Humanoid
local walk = enhum.Walk
local loadwalk = enhum:LoadAnimation(walk)
local attack = enhum.Attack
local loadattack = enhum:LoadAnimation(attack)
while wait() do
for i, v in pairs(game.Players:GetChildren()) do
local char = game.Workspace:WaitForChild(v.Name)
function attack()
if (script.Parent.HumanoidRootPart.Position - char.HumanoidRootPart.Position).magnitude < 6 then
if char.Parent ~= nil then
loadattack:Play()
char.Parent.Humanoid.Health = char.Parent.Humanoid.Health - 5
end
wait(0.4)
end
end
loadwalk:Play()
attack()
enhum.MoveToFinished:Wait()
loadwalk:Stop()
end
end
local players = game:GetService("Players")
function died()
local db = false
script.Parent.HitPart.Touched:Connect(function(otherpart)
if db == false then
if otherpart.Name == "Handle" then
db = true
local player = players:GetPlayerFromCharacter(otherpart.Parent.Parent)
print(player)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
end
if otherpart.Parent:FindFirstChild("Humanoid") and otherpart.Parent:FindFirstChild("Health") then
db = true
local player = players:GetPlayerFromCharacter(otherpart.Parent)
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + 1
end
end
end)
loadwalk:Stop()
local torso = script.Parent.Torso
torso:Destroy()
wait(5)
script.Parent:Destroy()
end
enhum.Died:Connect(died)
everything in the model has anchor off and the humanoidrootpart has cancolide off. Im not sure if it matters but I have another group in it, because I cant union something.