of     1   

MightyDantheman
#184273427Thursday, February 25, 2016 12:11 AM GMT

How would I find the smallest value in a group of values? I'm using this to find the closest player to an object. So far, I've got: for _, player in pairs(game.Players:GetPlayers()) do print(player.Character.Torso.Position) end --// How would I put all the different values (distance for each player) into a table, then find the smallest value? If there is an easier way, please tell me. ~MightyDantheman
JarodOfOrbiter
#184273484Thursday, February 25, 2016 12:11 AM GMT

If it is numbers, you can use math.min. Otherwise, you can use table.sort with a custom sort function.
MightyDantheman
#184273977Thursday, February 25, 2016 12:20 AM GMT

I tried to do the following: for _, player in pairs(game.Players:GetPlayers()) do print(math.min(player.Character.Torso.Position)) end --// But it says: Workspace.NPC.RunPhase:8: bad argument #1 to 'min' (number expected, got Vector3) I know what that means, but how would I change the vector into a number that I could use? Or should I try a different way of measurement? ~MightyDantheman
electricpretzel0
#184274134Thursday, February 25, 2016 12:23 AM GMT

Try this obj=//directory to your object people are getting close to local playerNames={} local playerDistances={} for i, player in pairs(game.Players:GetPlayers()) do playerNames[i]=player.Name playerDistances[i]=(player.Torso.Position-obj.Position).magnitude end wait for the other half
electricpretzel0
#184274171Thursday, February 25, 2016 12:23 AM GMT

local closestDistance=99999 local closestPlayer=""
electricpretzel0
#184274242Thursday, February 25, 2016 12:25 AM GMT

omg ROBLOX won't let me post my code UGFGHGHHHG
MightyDantheman
#184274811Thursday, February 25, 2016 12:35 AM GMT

I'm a bit confused, but this is what I've got so far: local phase = script.Parent.Phase local h = script.Parent.Humanoid while true do game.Workspace:WaitForChild("Player") local plr = game.Workspace:FindFirstChild("Player") for _, player in pairs(game.Players:GetPlayers()) do local mag = (player.Character.Torso.Position - script.Parent.Torso.Position).magnitude print(math.min(mag)) if phase.Value == "Run" then tor = script.Parent.Torso tar = plr.Torso local newCF = CFrame.new(tor.Position,tar.Position) local direction = newCF.lookVector * -1 h:Move(direction) end end wait() end --// But even with math.min, I wouldn't be able to find the player that belongs to that number. I'm guessing I will have to make a table, then? ~MightyDantheman
MightyDantheman
#184275300Thursday, February 25, 2016 12:46 AM GMT

Bump.. ~MightyDantheman
MightyDantheman
#184276583Thursday, February 25, 2016 1:09 AM GMT

RIP x.x ~MightyDantheman
TimeTicks
#184276930Thursday, February 25, 2016 1:15 AM GMT

table.sort(dist,function(a,b) return a < b end)
MightyDantheman
#184279169Thursday, February 25, 2016 1:55 AM GMT

@Above I'm not sure that helps my cause? I've changed things up a bit: while true do for _, player in pairs(game.Players:GetPlayers()) do local mag = (player.Character.Torso.Position - script.Parent.Parent.Torso.Position).magnitude print(math.min(mag)) end wait() end --// Once I get a way to find the closest player, it would change the value of it's parent. ~MightyDantheman
TaaRt
#184279824Thursday, February 25, 2016 2:05 AM GMT

local smallest = {math.huge,nil} for _,v in pairs(game.Players:GetPlayers()) do local cur = v:DistanceFromCharacter(target.Position) if cur < smallest[1] then smallest[1] = cur smallest[2] = v end end smallest[2] will contain the closest player
MightyDantheman
#184284328Thursday, February 25, 2016 3:27 AM GMT

Thank you. Everything works fine with this script except for one thing. When I test this with a 2 player server (through studio), it gives the error that there wasn't a player to be selected. Here is the script: wait(1) while true do local smallest = {math.huge,nil} for _,v in pairs(game.Players:GetPlayers()) do local cur = v:DistanceFromCharacter(script.Parent.Parent.Torso.Position) if cur < smallest[1] then smallest[1] = cur smallest[2] = v end end script.Parent.Value = smallest[2].Name wait() end --// Is there a way to make the script wait until at least one player has entered the game? I've tried "game.Players:WaitForChild()", but that comes up with another error. Any ideas? ~MightyDantheman
TaaRt
#184296396Thursday, February 25, 2016 12:38 PM GMT

wait(1) while true do while #game.Players:GetPlayers() < 1 do wait(1) end local smallest = {math.huge,nil} for _,v in pairs(game.Players:GetPlayers()) do local cur = v:DistanceFromCharacter(script.Parent.Parent.Torso.Position) if cur < smallest[1] then smallest[1] = cur smallest[2] = v end end script.Parent.Value = smallest[2].Name wait() end

    of     1