generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Finding the Smallest Value?

Previous Thread :: Next Thread 
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
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
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
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
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
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
electricpretzel0 is not online. electricpretzel0
Joined: 30 Sep 2008
Total Posts: 1278
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
electricpretzel0 is not online. electricpretzel0
Joined: 30 Sep 2008
Total Posts: 1278
24 Feb 2016 06:23 PM
local closestDistance=99999
local closestPlayer=""
Report Abuse
electricpretzel0 is not online. electricpretzel0
Joined: 30 Sep 2008
Total Posts: 1278
24 Feb 2016 06:25 PM
omg ROBLOX won't let me post my code UGFGHGHHHG
Report Abuse
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
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
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
24 Feb 2016 06:46 PM
Bump..


~MightyDantheman
Report Abuse
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
24 Feb 2016 07:09 PM
RIP x.x


~MightyDantheman
Report Abuse
TimeTicks is not online. 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
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
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 is not online. 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
MightyDantheman is not online. MightyDantheman
Joined: 06 May 2011
Total Posts: 5108
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image