Check if player is far away from group

Imagine there is a group of cars, driven by players. One of them brakes, and they are now 50+ studs away from others. How can I detect this?
Untitled
Another example is when everyone brakes except one person, that one person will get their car removed because they would be 50+ studs away from the group but in front of them instead of behind them.

I have absolutely no idea on how to do this, I’m not trying to ask for a script but I’m trying to ask how I can do this. Thanks.

3 Likes

Well I mean you could technically just get the magnitude of the closest car, you dont have to get the magnitude of every single one in the group

edit : I am guessing that the group of cars will stay close together so theres no point in checking al-l the magnitudes of all the cars @calvin_coco

2 Likes

Usually you would use Magnitude, but i dont know if thats what you want

1 Like

@calvin_coco

Ok so i believe i found a way with Models, you might be able to use WorldPivot which is pretty much the Position of the Model?
(More like a copy of the Position since it doesnt actually change the position when you do so, only when you change the origin)

plr = game.Players.LocalPlayer
char = plr.Character or plr.CharacterAdded:Wait()



while true do
	wait()
	local Mag = (workspace.Model.WorldPivot.Position - char.HumanoidRootPart.Position).Magnitude
	
	if Mag > 50 then
		print("Player is far away")
	end
end

The only issue is that the Bounding box will resize if a item moves away


Another Solution is that you may be able to Apply a Tag to the Leading Person in the group, an detect if you are far away from there

1 Like

What is this for? By ‘remove’ do you mean the player loses if they are too far behind? If so, it seems kind of unfair.

1 Like

Get all players in the game and store in an array P.
Use in ipairs to loop thru array P.
For each player in array P, compare the distance with all other players in array P.
If distance are all > 50, then player is out.

2 Likes

Yes, you are right, maybe I should just teleport them back to the group.

1 Like

What do you mean by “closest car”? Closest to what?

1 Like

No, that’s not really what I mean. That just checks if the player is far away from the car. I mean if the car is far away from the group.

Thanks! If you need it, here’s my script.

local list = workspace.Cars:GetChildren()

for _, plr in list do
    local closestDist = math.huge

    for _, otherPlr in list do
        local dist = plr:DistanceFromCharacter(otherPlr.Character.HumanoidRootPart.Position)

        if otherPlr ~= plr and dist < closestDist then
            closestDist = dist
        end
    end

    if closestDist >= 50 then
        -- is far away from group
    end
end
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.