Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How To Find Table Value's Position?

Asked by
enes223 327 Moderation Voter
4 years ago

Im trying to sort player join order in a local script but I cant figure it out and I thought about this bbut I dont know how to make this can someone help me?

0
This an xy problem. You actually want to sort player join order but you instead ask how to find a table's value position programmerHere 371 — 4y

2 answers

Log in to vote
1
Answered by
royaltoe 5144 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Hi, the table will already be sorted based on when people join. table.insert will add the player's name at the end of the table. The if statement you have inside of the playeradded function will not work as the player joining will always be the player you need to add to the array.

gtg so i didnt get to test this: Serverscript:


local sortedPlayers = {} local Text = "" function getIndex(playerName) for i = 1, #sortedPlayers do if(sortedPlayers[i] = playerName)then return i end end end game.Players.PlayerAdded:Connect(function(plr) --player who just entered will never not be player table.insert(sortedPlayers, plr.Name) end) game.Players.PlayerRemoving:Connect(function(plr) table.remove(sortedPlayers, getIndex(plr.Name)) end) while(wait(1))do Text = "Players: " --loop through the sorted list of players --i is the position in the table for i = 1, #sortedPlayers do Text = Text .. " " ..sortedPlayers[i] end print(Text) end
0
Ill try when I get the laptop :D enes223 327 — 4y
0
Ok tell me how it goes royaltoe 5144 — 4y
0
Thanks so much! enes223 327 — 4y
0
pls mark it as solved and upvote since it worked royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by
enes223 327 Moderation Voter
4 years ago

Script:

--By enes223
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("UserSender")

local Players = {}

local Text = ""

game.Players.PlayerAdded:Connect(function(plr)
    if not Players[plr.Name] then
        table.insert(Players,plr.Name)
        for i,v in pairs(Players) do
            Text = Text..v..","
            Remote:FireClient(plr,Text)
            Remote:FireAllClients(Text)
        end
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    if Players[plr.Name] then
        Players[plr.Name] = nil
        for i,v in pairs(Players) do
            Text = Text..v..","
            Remote:FireAllClients(Text)
        end
    end
end)

Local Script:

--By enes223
local Sorter = game:GetService("ReplicatedStorage"):WaitForChild("Sorter")
local Remote = game:GetService("ReplicatedStorage"):WaitForChild("UserSender")

local U1,U2,U3,U4,U5 = script.Parent.User1,script.Parent.User2,script.Parent.User3,script.Parent.User4,script.Parent.User5
local I1,I2,I3,I4,I5 = U1.Index,U2.Index,U3.Index,U4.Index,U5.Index

local SortedTable = {}
local Indexes = {I1,I2,I3,I4,I5}

function Seperate(Text,Seperator)
    local SubNumber = 1
    local Line = Text:sub(1,SubNumber)
    repeat
        Line = Text:sub(1,SubNumber)
        SubNumber = SubNumber + 1
    until Line:find(Seperator)
    if Line:find(Seperator) then
        return Line:gsub(Seperator,"")
        end
end

function Init(Text)
            if not Text:find(",") then
            for i,v in pairs(SortedTable) do
                for j,k in pairs(Indexes) do
                    local Index = #Indexes
                    if k.Value == Index then
                        k.Parent.Image = game.Players:GetUserThumbnailAsync(game.Players[v].UserId,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size60x60)
                        k.Parent.User.Text = v
                        Index = Index - 1
                        end
                end
            end
        else
            if not SortedTable[Seperate(Text,",")] then
            SortedTable[Seperate(Text,",")] = Seperate(Text,",") 
            Text = Text:gsub(Seperate(Text,",")..",","")
            Init(Text)
            end
        end
end

Remote.OnClientEvent:connect(function(Text)
    Init(Text)
end)

Answer this question