for _, 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
print(Player.Name .. " is on range")
warn('Attack')
break
end
end
end
This script obtains the player in order
Only the same person will be attacked all the time.
Is there a way to get someone to attack randomly?
You can check assign all players in range to a table, then pick a random player from that table.
local Players = game:GetService("Players")
local Boss = ... -- change this to your boss character
local Range = 50 -- adjust this as needed
local function findRandomPlayerInRange()
local playersInRange = {}
for _, 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
table.insert(playersInRange, Player)
end
end
end
if #playersInRange > 0 then
local randomIndex = math.random(1, #playersInRange)
return playersInRange[randomIndex]
else
return nil
end
end
-- Example usage:
local randomPlayer = findRandomPlayerInRange()
if randomPlayer then
print(randomPlayer.Name .. " is on range.")
-- Attack randomPlayer
else
print("No players in range.")
end