I am making a enemy that only runs after you when you are in a certain area. I will explain what I need help with in the notes in the script
local rig = script.Parent
local electricarea = script.Parent.Parent["Electric Area"]
local function getplayer()
local region = Region3.new(electricarea.Position - (electricarea.Size/2), electricarea.Position + (electricarea.Size/2))
local parts = workspace:FindPartsInRegion3(region,nil,100000)
local playerspos = {}
for i,v in pairs(parts) do
if v.Parent:FindFirstChild("Humanoid") then
table.insert(playerspos,v.Parent.HumanoidRootPart.Position)
return playerspos
end
end
end
while wait(1) do
local distances = {}
for i ,v in pairs(getplayer()) do
local distance = (rig.HumanoidRootPart.Position - Vector3.new(getplayer())).Magnitude
table.insert(distances, distance)
end
local clostestdistance = table.unpack(distances)
rig.Humanoid:MoveTo(Vector3.new(clostestdistance)) -- when i get .magnitude, it only gives me one position, so it will go far away, but i want to find the closest position, still having the position
print(clostestdistance)
end
I probably explained this horrible, so ask about stuff ig
ive already figured that part out using regions, the part in the script where i use magnitued is when i find the closest character to the boss, under the while loop
Thankyou! Have you found a solution yet? I thought I could maybe add the magnitude onto the x and z axises but i didnt know which distance on the table was the one I should add it onto
.Magnitude isn’t doing anything wrong here, and I’m 100% sure there aren’t any better or less complicated methods to find the closest player that doesn’t use it. Your algorithm to search for the nearest player is just not done correctly.
EDIT: Also, I don’t think your player searching algorithm works well either, it seems to only be getting one single player’s position so you don’t actually have anything to compare it to
Also table.unpack doesnt sort the magnitude, you are doing a lot of things wrong. You want to compare the players magnitudes, to find the closest one. Then walk your mob to the player not to the magnitude but the players position
the reason i have it like this, is becuase there are two sides of the area i built, and when the player crosses over to the other side, i want it to stop chasing the player
Your “getplayer()” function only returns a table with 1 position, so you can’t actually find the closest position if you don’t have any others to compare it to.
Fix that by removing the ‘return’ and adding some extra checks to prevent the loop from finding the same player twice
Your algorithm to find the closest player in the list returned by getplayer() is also wrong.
It’s not getting the closest distance as you aren’t actually comparing anything (check the post I tagged in my previous post for a good algorithm) and the MoveTo vector3 doesn’t actually go to wherever the player is because you haven’t stored the actual position anywhere, only the total distance.
i will fix the rest of it thanks! but I am trying to get the actual position, because i only know the magnitude, i dont know which player i got that magnitude from
Aight im gonna correct it in parts cause im at work rn
First off get plager should simple just get the players or get the once that are close
function getPlayerCharacters():
local characters = {}
for _, v in game:GetService("Players"):getPlayers()
local char = v.Character
If char then
table.insert(characters, v)
end
end
local rig = script.Parent
local electricarea = script.Parent.Parent["Electric Area"]
local region = Region3.new(electricarea.Position - (electricarea.Size/2), electricarea.Position + (electricarea.Size/2))
local function movetonearestplayer()
local parts = workspace:FindPartsInRegion3(region,nil,100000)
local nearestPlayer, nearestDistance
for i,v in pairs(parts) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent:FindFirstChild("Health") then
local character = v.Parent
print(character)
local player = game:GetService("Players"):GetPlayerFromCharacter(character)
local distance = player:DistanceFromCharacter(rig:WaitForChild("HumanoidRootPart").Position)
if not character or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
end
local char = nearestPlayer.Character or nearestPlayer.CharacterAdded:Wait()
rig:MoveTo(Vector3.new(char:WaitForChild("HumanoidRootPart").Position))
end
local iflightning = 10
while wait(1) do
movetonearestplayer()
end
he goes really far away for no reason, but with nearest player, i dont get when it found it, it just found the nearest distance and then set the nearest player valuable to the player in the for loop