|
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 |
|
|
If it is numbers, you can use math.min. Otherwise, you can use table.sort with a custom sort function.
|
|
|
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 |
|
|
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
|
|
|
local closestDistance=99999
local closestPlayer=""
|
|
|
omg ROBLOX won't let me post my code UGFGHGHHHG |
|
|
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 |
|
|
|
TimeTicksJoin Date: 2011-04-27 Post Count: 27115 |
table.sort(dist,function(a,b)
return a < b
end) |
|
|
@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 |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
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 |
|
|
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 |
|
TaaRtJoin Date: 2009-04-26 Post Count: 5070 |
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
|
|