|
| 07 May 2014 04:24 PM |
| I'm trying to write a script to find the closest player to a block, and do something accordingly, but I don't know where to start. |
|
|
| Report Abuse |
|
|
|
| 07 May 2014 04:26 PM |
| maybe do: (block)=(the stud number)(player)change |
|
|
| Report Abuse |
|
|
|
| 07 May 2014 04:27 PM |
| Do you mean closest to anywhere on the block? like if it was the baseplate, it woul measure how far away you are from the surface? |
|
|
| Report Abuse |
|
|
|
| 07 May 2014 04:34 PM |
Nah, Im talking small scale, like literally a 1x1 block, and how far away all players are from it, then it gets the closest one and then the rest is up to me :P
|
|
|
| Report Abuse |
|
|
|
| 07 May 2014 04:45 PM |
| Loop through the players, measure the distance to their torso with (Part.Position-Torso.Position).magnitude, and at the same time, see if it's less than the smallest distance so far. If it is, update the currently closest player variable and their distance. |
|
|
| Report Abuse |
|
|
RoflBread
|
  |
| Joined: 18 Jun 2009 |
| Total Posts: 3803 |
|
|
| 07 May 2014 04:57 PM |
| Anyone else notice the 45k views? |
|
|
| Report Abuse |
|
|
| |
|
|
| 07 May 2014 06:01 PM |
| someone did that on my thread also :/ |
|
|
| Report Abuse |
|
|
|
| 07 May 2014 06:08 PM |
You would use magnitude.
table = {}
for i,v in pairs(game.Players:GetChildren()) do while wait() do local m = (v.Character.Torso.Position-workspace.Part.Position) table.insert(table,m) table.sort(table) print(table[1]) end end |
|
|
| Report Abuse |
|
|
|
| 07 May 2014 06:09 PM |
table = {}
for i,v in pairs(game.Players:GetChildren()) do while wait() do local m = (v.Character.Torso.Position-workspace.Part.Position).magnitude table.insert(table,m) table.sort(table) print(table[1]) end end
Sorry, messed up. |
|
|
| Report Abuse |
|
|
|
| 08 May 2014 04:57 AM |
| Thanks for trying cooldude, but it's not working properly. It's printing the value of the nearest player's torso from the block I have it set to, and even then it only updates when I get closer. What I needed it to do was find the closest player (at all times, even after someone leaves or joins), and then I can do something only to that closest player. |
|
|
| Report Abuse |
|
|
vlekje513
|
  |
| Joined: 28 Dec 2010 |
| Total Posts: 9057 |
|
| |
|
| |
|
|
| 08 May 2014 06:27 AM |
table = {}
for i,v in pairs(game.Players:GetChildren()) do while wait() do local m = (v.Character.Torso.Position-workspace.Part.Position).magnitude table.insert(table,m.." "..v.Name table.sort(table) print(table[1]) end end
Try this. |
|
|
| Report Abuse |
|
|
|
| 08 May 2014 06:28 AM |
table = {}
for i,v in pairs(game.Players:GetChildren()) do while wait() do local m = (v.Character.Torso.Position-workspace.Part.Position).magnitude table.insert(table,m.." "..v.Name) table.sort(table) print(table[1]) end end
Fixed an error. |
|
|
| Report Abuse |
|
|
EcIiptic
|
  |
| Joined: 12 Aug 2009 |
| Total Posts: 13737 |
|
|
| 08 May 2014 06:46 AM |
@Cool
That simply doesn't work.
Due to the fact that there's a while loop in your for loop..
It would only go to one player and never to the next.
You could use a coroutine though.
for i, v in pairs(game.Players:GetChildren()) do wrapp = coroutine.wrap(function()
end) wrapp() end
#Piano |
|
|
| Report Abuse |
|
|