How to make shooting NPC

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!

1 Like

I am sorry to say I cannot see anything obviously wrong with the code.
Could you put some prints in to show the logic flow is what you expect.
For example what is the value of dist?

Instead of posting one function, please post the entire script. We have no idea of what the environment of this function is, and what any of your variables are.

Also remember to disconnect your events. :slight_smile:

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

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
like this

in the for loop of players I would like to see a print(dist) and when run the values displayed in the console window.

Error in output: Workspace.Slime.AI:66: invalid argument #3 (CFrame expected, got Vector3)

so line 66 is passing 3 items and the 3rd item should be a CFrame and is in fact a Vector3.
I cannot tell from the code presented which is line 66 but you could replace the 3rd item with CFrame.new(TheVector3.X,TheVector3.Y,TheVector3.Z)
where TheVector3 is the current 3rd item.