When you want to spawn the part, you would iterate over the player’s fighting the boss and store a reference to the player, as well as the magnitude of the vector from the player to the boss (in vector theory, magnitude is referred to as the length of a vector) in a new array (a table with only numeric indices) in a table holding all of this information about all of the players:
local info = {}
for _, player in pairs(players fighting the boss) do
local char = player.Character
if char then
info[#info + 1] = {player, (char.HumanoidRootPart.Position - boss.HumanoidRootPart.Position).Magnitude}
end
end
Now that we have all of the magnitudes of the players’ positions from the boss, you’ll have to sort the table from lowest to highest based on magnitude. The lowest magnitude among all is the closest/nearest position and we can achieve this using table.sort:
table.sort(info, function(a, b)
return a[2] < b[2]
end)
The function above sorts the table from lowest to highest, so now that that’s done, we can retrieve the closest player by just looking at the first entry of the table, since table.sort sorted the table from lowest to highest:
local closestPlayerINFO = info[1]
local closestPlayer = closestPlayerINFO[1]
local closestMagnitude = closestPlayerINFO[2]
print(closestPlayer.Name, closestMagnitude)
Now that the closest player is found, you can now spawn a part above their head
Wait, so do I just make the position of the bullet be the “closestPlayer”?
local info = {}
for _, player in pairs() do
local char = player.Character
if char then
info[#info + 1] = {player, (char.HumanoidRootPart.Position - boss.HumanoidRootPart.Position).Magnitude}
end
end
table.sort(info, function(a, b)
return a[2] < b[2]
end)
local closestPlayerINFO = info[1]
local closestPlayer = closestPlayerINFO[1]
local closestMagnitude = closestPlayerINFO[2]
function fire()
script.Parent.BrickColor = BrickColor.new("Bright red")
bullet = Instance.new("Part", workspace)
bullet.Shape = Enum.PartType.Ball
bullet.BrickColor = BrickColor.new("Neon orange")
bullet.Material = Enum.Material.Neon
bullet.Position = closestPlayer
wait(3)
bullet:Destroy()
end
while true do
wait(1)
fire()
end
Also, you can reply to people by clicking the reply button on their post or mentioning them for that person to get a notification that they were replied to. If you don’t, then the user won’t get the message
You have to put a table as an argument for pairs()! As @Raretendoblox explained, that table must contain all the players fighting the boss. It is up to you to decide if someone can be affected by the boss or not.
local Boss = script.Parent
while wait(0.1) do
for i, v in pairs(game.Workspace:GetChildren()) do
if v:FindFirstChild("HumanoidRootPart") then
local HRP = v:FindFirstChild("HumanoidRootPart")
local Head = v:FindFirstChild("Head")
if (Boss.Position - HRP.Position).Magnitude <= 10 then
print("Teleporting Boss to the player")
Boss.Position = Vector3.new(Head.Position)
local OffsetVector = Vector3.new(0, 2, 0)
Boss.Position = Head.Position + OffsetVector
end
end
end
end
Well, I assume that you would want to make use of a simple search to find the nearest player, and place a block above your head. I wrote this quickly. Hope you can learn from it:
--[[
NOTE: This script assumes that the Boss is merely a position in the world. You can reference a Boss Model and a part within it to use as a reference.
I could use ":GetPrimaryPartCFrame()" here, but assuming you are not aware of this method I shall resort to plain old .Position for the time being,
but please do read up on this and see if you could implement it on your own to nurture your own learning.
--]]
local Players = game:GetService("Players"); --Fetch the player service
local BossPart = workspace:WaitForChild("BossPart"); --The "Boss"
local function FindNearestPlayer()
--Here we are searching through every player in the game.
local NearestPlayer, NearestDistance; --Here, we track two things. Nearest Player, and Nearest Distance. We do this to see WHO is the closest and by how much.
local FirstTime = true; --Just a bool to tell the script it's our first time searching and to not compare anything until a second cycle.
for _, Player in pairs(Players:GetPlayers()) do
local Character = Player.Character;
if Character and Character.PrimaryPart then --Check that they have a character model and a part to reference their distance by.
local PlayerDistance = (Character.PrimaryPart.Position - BossPart.Position).Magnitude;
--[[
To calculate the Player's Distance from the boss, we use the "Magnitude" property of a Vector3 value. Read more about this here:
https://developer.roblox.com/en-us/articles/Magnitude
--]]
if FirstTime then
--If the first time, then we want to initially set the nearest variables to the player we're referencing as we have nothing else to go off of.
NearestPlayer, NearestDistance = Player, PlayerDistance;
FirstTime = false; --We've passed the first cycle, so we don't want to enter this if statement again.
else
if PlayerDistance < NearestDistance then
--When looping through, if we find a player closer than our current closest player, we update our nearest variables.
NearestPlayer, NearestDistance = Player, PlayerDistance;
end
end
end
end
--Once this loop is finished, we should end up with a player that is closest to the boss.
--We now want to return this player back to the script so we can use it to place a part on their head!
return NearestPlayer;
end
local function SpawnPartOnNearestPlayer()
--First, we need the nearest player. We get it with this custom searching algorithm!
local NearestPlayer = FindNearestPlayer();
if NearestPlayer then --It may not exist, so make sure to check for that.
--Here I take the position of the head of the closest player, and move 10 studs above it to place my part.
local PositionToPlacePart = NearestPlayer.Character.PrimaryPart.Position + Vector3.new(0, 10, 0);
local NewPart = Instance.new("Part");
NewPart.Anchored = true; --We don't want it to fall!
NewPart.Position = PositionToPlacePart; --Put it above the head.
NewPart.Parent = workspace; --Now put it in to the game.
end
end
while true do
SpawnPartOnNearestPlayer(); --Spawn a part on the nearest player constantly.
wait(0.3); --You could wait any time but I recommend a relatively mild frequency as we don't want things to be too laggy.
end
I also attached the file of the place I demonstrated this in, along with a gif of it functioning: