Npc Boss attack system

Hi, can anybody give me an easy tutorial on making a boss NPC that attacks the player near its range?

6 Likes
local Range = 50
local Distance = (Boss.HumanoidRootPart.Position - Target.HumanoidRootPart.Position).Magnitude

if Distance <= Range then
	-- stuff
end
1 Like

Thanks, I will try it and let you know the result.

I used the target this way, but I think the script is not detecting the Players humanoid.

local Players = game:GetService("Players")
local Boss = game.Workspace.Boss
local Range = 50
local Distance = (Boss.HumanoidRootPart.Position - Players.HumanoidRootPart.Position).Magnitude

if Distance <= Range then
	

end

I think I need to search the entered parts parent and set it as a player. Idk.

its the players service, not a character

1 Like

I am new to scripting so I am kinda confused right now. Can u also help me make it like whenever the player enters the boss range the health of the player starts to decrease?

I am trying but I am clueless.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
local Boss = game.Workspace.Boss
local Range = 50
local Distance = (Boss.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

if Distance <= Range then
			
			
		end
	end)
end)

Your only checking it once. This means that the code will only run once. Try using a loop

full script then

local Players = game:GetService("Players")
local Boss = game.Workspace.Boss
local Range = 50

local function Attack1()
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		
		if Target:FindFirstChild("HumanoidRootPart") then
			local Distance = (Boss.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

			if Distance <= Range then
				-- stuff
				print(Player.Name .. " is on range")
			end
		end
	end
end

while task.wait(10) do
	Attack1()
end
1 Like

image
I don’t know why but the output feed is not printing my name.

1 Like

oops, change “Character” on line 10 to “Target”

local Players = game:GetService("Players")
local Boss = game.Workspace.Boss
local Range = 50

local function Attack1()
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		
		if Target:FindFirstChild("HumanoidRootPart") then
			local Distance = (Boss.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

			if Distance <= Range then
				-- stuff
				print(Player.Name .. " is on range")
			end
		end
	end
end

while task.wait(10) do
	Attack1()
end

I changed this “target” to Character and used it but the script is not working. Can u check it on your studio if possible?
image

ItsWorkingItsWorkingItsWorkingItsWorking

local Players = game:GetService("Players")
local Boss = game.Workspace.Boss
local Range = 50

local function Attack1()
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character
		
		if Character and Character:FindFirstChild("HumanoidRootPart") then
			local Distance = (Boss.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

			if Distance <= Range then
				-- stuff
				print(Player.Name .. " is on range")
			end
		end
	end
end

while task.wait(10) do
	Attack1()
end

I suck because I was not scripting on studio

2 Likes

Imma be real with you chief, if you can’t make this system by yourself, you realistically have 2 courses of action:

  1. Take a break from development to get better at scripting
  2. Hire someone

The dev forum is a place for help with little things or bugs you don’t know how to fix, not a place to craft entire systems for you. Unless you’re hiring people, you need to have a solid grasp of what your doing to succeed. Wait until your no longer “new to scripting” before you bite off more than you can currently chew.

2 Likes

Sorry, I got carried away, I was just asking if someone can provide me with a complete guide where I can learn this enemy attack system because I couldn’t find any proper one that can help me with what I am trying to achieve. But Ping gave me the perfect script that I was looking for (I know it should be working but I don’t know why it is not working) and I got carried away with it.

1 Like

Thanks for the help till now, I can tell the script is perfect so I will try to figure out why it is not working for me.

Ok so it wasnt working for me because I misunderstood the range value. I played around with it for a bit by lowering and highering the value until I found the value I needed. Now It is working. Thank you.

This is the final product for the people who are also looking for something like this. What this script will do is whenever you will go near a boss which has a Humanoid and a HumanoidRootPart in it, your health will starts to decrease. after every 10 seconds.

local Players = game:GetService("Players")
local Boss = game.Workspace.Boss
local Range = 20

local function Attack1()
	for i, Player in pairs(Players:GetPlayers()) do
		local Character = Player.Character

		if Character and Character:FindFirstChild("HumanoidRootPart") then
			local Distance = (Boss.HumanoidRootPart.Position - Character.HumanoidRootPart.Position).Magnitude

			if Distance <= Range then
				Character:FindFirstChild("Humanoid").Health -= 10
				print(Player.Name .. " is on range")
			end
		end
	end
end

while task.wait(10) do
	Attack1()
end
1 Like