|
| 24 Feb 2016 06:11 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:11 PM |
If it is numbers, you can use math.min. Otherwise, you can use table.sort with a custom sort function.
|
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:20 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:23 PM |
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
|
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:23 PM |
local closestDistance=99999 local closestPlayer=""
|
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:25 PM |
| omg ROBLOX won't let me post my code UGFGHGHHHG |
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 06:35 PM |
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 |
|
|
| Report Abuse |
|
|
| |
|
| |
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 24 Feb 2016 07:15 PM |
table.sort(dist,function(a,b) return a < b end) |
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 07:55 PM |
@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 |
|
|
| Report Abuse |
|
|
TaaRt
|
  |
| Joined: 26 Apr 2009 |
| Total Posts: 5039 |
|
|
| 24 Feb 2016 08:05 PM |
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 |
|
|
| Report Abuse |
|
|
|
| 24 Feb 2016 09:27 PM |
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 |
|
|
| Report Abuse |
|
|
TaaRt
|
  |
| Joined: 26 Apr 2009 |
| Total Posts: 5039 |
|
|
| 25 Feb 2016 06:38 AM |
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
|
|
|
| Report Abuse |
|
|