Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

I need it to pinpoint the closests person?

plr = game.Players.LocalPlayer
chr = plr.Character
mouse = plr:GetMouse()
p = Instance.new("Part", workspace)
p.CFrame = CFrame.new(10,10,10)
p.Anchored = true
p.Name = "p"
p.Size = Vector3.new(3,3,3)

while true do
        for i,v in pairs(game.Players:GetChildren()) do
        ded = v.Character.Torso.Position
                if (ded - p.Position).magnitude < 1 then
                        p.CanCollide = false
                        p.Transparency = 0.5
                else
                        p.CanCollide = true
                        p.Transparency = 0
                
                print("Mouse pressed!")
                local ray = Ray.new(p.CFrame.p, (ded - p.CFrame.p).unit * 300)
                local part, position = workspace:FindPartOnRay(ray, plr.Character, false, true)
 
                local beam = Instance.new("Part", workspace)
                beam.BrickColor = BrickColor.new("New Yeller")
                beam.FormFactor = "Custom"
                beam.Material = "Neon"
                beam.Transparency = 0.25
                beam.Anchored = true
                beam.Locked = true
                beam.CanCollide = false
 
                local distance = (p.CFrame.p - position).magnitude
                beam.Size = Vector3.new(0.3, 0.3, distance)
                beam.CFrame = CFrame.new(p.CFrame.p, position) * CFrame.new(0, 0, -distance / 2)
 
                game:GetService("Debris"):AddItem(beam, 0.1)
end
        end
        wait()
end

I only want it to pinpoint the closest player, but I do not know how to do this!

1 comment

0

Have you heard of :DistanceFromCharacter()? I think it's a function of player.

1 answer

Create two variables: closest player (set to nil) and shortest distance (set to the maximum range, or math.huge for infinite) Loop through all players and: If the player has a character and the player's character has a torso and the player's character's torso's distance to the point is less than the shortest distance: Set closest player to the player and shortest distance to the distance between the player's character's torso and the point. Once the loop is complete you will have the closest player (or nil if no players found) and the distance to them.

This discussion is preserved as it was. New answers, comments, and votes are closed.