AespectJoin Date: 2012-04-24 Post Count: 3080 |
Hi,
I've tried and tried to make a table where it takes the player's positions and orders them from one to four. Everything works, except that it put the first place player in every slot. Here's the main script:
for _,v in pairs (Workspace:GetChildren()) do
if v:FindFirstChild("Athlete",true) then
v.Athlete.WalkSpeed = 25 --game.Players:FindFirstChild(v.Name).Speed.Value
end
end
startTime=tick()
repeat
wait(0.001)
MainG.Value = "100m Sprint ["..timer(tick()-startTime).."]"
for _,v in pairs(game.Players:GetChildren()) do
if v.Going.Value then
pcall(function() table.insert(temp,{--inserts into table
['player']=v;--racer
['pos']= math.abs(v.Character.Torso.Position.X);--distance
}); end); end end
selection_sort(temp);
_G.lineup=temp;
until
Racing==0 |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Apparently, I need cnt or island or another genius for this one... :/ |
|
|
Why not set up your table like this:
{
player1 = position,
player2 = position,
etc...
}
And then just cycle through each player occasionally to update and sort their positions.
-[::ƧѡÎḾḠΰῩ::]-[::Helper of Scripting and Writer of Wikis::] |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
1 |
|
C_SharperJoin Date: 2011-10-03 Post Count: 9405 |
Might be harder but you can definitely make it work.
Use magnitude and do a simpler set up with loops. |
|
|
table.sort(tab, function(a, b) return a.pos < b.pos end) |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Here's the sorting part. Might be something in here??
function selection_sort(tab)
for i = 1, #tab do
local minimum = i
for t = i + 1, #tab do
if tab[t].pos < tab[minimum].pos then
minimum = t
end
end
tab[i], tab[minimum] = tab[minimum], tab[i]
end
end |
|
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Erm, I'm not quite sure how to do that.... :/ |
|
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
I can't figure it out..... I'm sorry.... :/ |
|
sparker22Join Date: 2010-03-11 Post Count: 846 |
|
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
I tried doing that. For some reason, I can't seem to tie it in with my current sorting script... |
|
C_SharperJoin Date: 2011-10-03 Post Count: 9405 |
How about this, you put positions (numbers) into tables, and do something like this:
if pos[1] > pos[2] then
--stuff
end |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
I'm sorry, but if you could try to tie this into the sorting script, I'd be much appreciative! c: |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Bump |
|
C_SharperJoin Date: 2011-10-03 Post Count: 9405 |
I'd say using magnitude is easier.
local mag = (game.Workspace.Athlete1.Torso.Position - game.Workspace.Athlete2.Torso.Position).magnitude
print(mag)
Magnitude is the distance between the two. |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Ok, but the current problem is in the sorting. |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
... |
|
C_SharperJoin Date: 2011-10-03 Post Count: 9405 |
I know. Sometimes SH is greatly inactive. Like my post. Sorry, but I failed you :( |
|
C_SharperJoin Date: 2011-10-03 Post Count: 9405 |
Have you heard of 'table.sort'
If you input positions in the table, it can sort them. May be something to check into. |
|
|
table.sort(table, function(a, b) return a.pos < b.pos end)
Something like that. |
|
AespectJoin Date: 2012-04-24 Post Count: 3080 |
Is there anything wrong with this?? This is what does the sorting:
function selection_sort(tab)
--Go through every position in the table
for i = 1, #tab do
--start minimum at current position
local minimum = i
--Go through the remaining elements that need to be sorted
for t = i + 1, #tab do
--compare current element to minimum
if tab[t].pos < tab[minimum].pos then
--t is the new minimum
minimum = t
end
end
--switch minimum with current position
tab[i], tab[minimum] = tab[minimum], tab[i]
end
end |
|
|
table.sort is far better. You should use it. |
|