Hi im trying to make a script where all players get a highlight if certain distance is met but I can’t just figure it out, i tried this only to realize it wouldnt work
for _, players in game.Workspace.MorhpedPeople:GetChildren() do
if (script.Parent:WaitForChild("HumanoidRootPart").Position - players:WaitForChild("HumanoidRootPart").Position).Magnitude <= 25 then
game.ReplicatedStorage.Storage.CrystalHL:Clone().Parent = players
end
end
end
The indentation is off. The code snippet should look like this:
for _, players in game.Workspace.MorhpedPeople:GetChildren() do
if (script.Parent:WaitForChild("HumanoidRootPart").Position - players:WaitForChild("HumanoidRootPart").Position).Magnitude <= 25 then
game.ReplicatedStorage.Storage.CrystalHL:Clone().Parent = players
end
end
How often are you running this script? Is this in a run service event? A while loop?
Can you be more specific with what you want? Based on the code, I’m guessing that you want a player to be highlighted if they are more than 25 studs away from the local player.
local CrystalHL = game.ReplicatedStorage
:WaitForChild("Storage"):WaitForChild("CrystalHL")
local peeps = game.Workspace:WaitForChild("MorhpedPeople")
for _, peep in pairs(peeps:GetChildren()) do
local hrp = peep:WaitForChild("HumanoidRootPart")
if(script.Parent:WaitForChild("HumanoidRootPart").
Position - hrp.Position).Magnitude <= 25 then
CrystalHL:Clone().Parent = peep
end
end
Okay, the code @2112Jay posted should mostly get you there. It just loops through all players and gives them another highlight every time an input is processed.
You may want to check another 2 things:
Only add a highlight if the player doesn’t already have a highlight
Remove highlights from players if they are outside the range
local CrystalHL = game.ReplicatedStorage
:WaitForChild("Storage"):WaitForChild("CrystalHL")
local peeps = game.Workspace:WaitForChild("MorhpedPeople")
for _, peep in pairs(peeps:GetChildren()) do
local hrp = peep:WaitForChild("HumanoidRootPart")
local existingHL = peep:FindFirstChild("CrystalHL")
local dist = (script.Parent:WaitForChild("HumanoidRootPart")
.Position - hrp.Position).Magnitude
if dist <= 25 and not existingHL then
CrystalHL:Clone().Parent = peep
elseif dist > 25 and existingHL then
existingHL:Destroy()
end
end