Hello! Im want make NPC shooting me. I’ve tried but this wont work. Can u help me?
local burned = script.Parent
local primarysetting = game.ReplicatedStorage.Difficulty.ChoosenDif.Burnt
local humanoid = burned.Zombie
local pathf = game:GetService("PathfindingService")
burned.PrimaryPart:SetNetworkOwner(nil)
local function canseetarget(target)
local origin = burned.HumanoidRootPart.Position
local direction = (target.HumanoidRootPart.Position - burned.HumanoidRootPart.Position).Unit * 40
local ray = Ray.new(origin, direction)
local hit, pos = workspace:FindPartOnRay(ray, burned)
if hit then
if hit:IsDescendantOf(target) then
return true
end
else
return false
end
end
local function findTarget()
local players = game.Players:GetPlayers()
local maxd = 50
local nearisttarget
for index, player in pairs(players) do
if player.Character then
local target = player.Character
local dist = (burned.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if dist < maxd and canseetarget(target) then
nearisttarget = target
maxd = dist
end
end
end
return nearisttarget
end
local function getpath(bwk1)
local pms = {
["AgentHeights"] = 5,
["AgentRadius"] = 5,
["AgentCanJump"] = true
}
local path = pathf:CreatePath(pms)
path:ComputeAsync(burned.HumanoidRootPart.Position, bwk1.Position)
return path
end
local function attack (target)
local dist = (burned.HumanoidRootPart.Position - target.HumanoidRootPart.Position).Magnitude
if dist > 30 then
humanoid:MoveTo(target.HumanoidRootPart.Position)
else
local attackanim = humanoid:LoadAnimation(script.hitanim)
attackanim:Play()
local projectile = game.ReplicatedStorage.Info.Slime_Projectile:Clone()
projectile.Parent = workspace
projectile.Position = burned.HumanoidRootPart.Position
projectile.CFrame = Vector3.new(script.Parent.HumanoidRootPart, target.HumanoidRootPart)
local velocity = Instance.new("BodyVelocity", projectile)
velocity.Velocity = projectile.CFrame.LookVector * 240
projectile.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
target.Humanoid:TakeDamage(35)
end
end)
end
end
local function walkto(bwk1)
local path = getpath(bwk1)
if path.Status == Enum.PathStatus.Success then
for index, waypoint in pairs(path:GetWaypoints()) do
local target = findTarget()
if target and target.Humanoid.Health > 0 then
attack(target)
break
else
humanoid:MoveTo(waypoint.Position)
humanoid.MoveToFinished:Wait()
end
end
else
humanoid:MoveTo(bwk1.Position - (burned.HumanoidRootPart.CFrame.LookVector * 10))
end
end
local function patrol()
local waypoints = workspace.BurnedPoints:GetChildren()
local random = math.random(1, #waypoints)
walkto(waypoints[random])
end
while wait(1) do
patrol()
end
Thank you in advance!