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

Why won't my code to make a list of all players in-game work?

Asked by 3 years ago

I want to make a GUI with a list of all players currently in-game, displaying info about the player such as their name, display name, and headshot image. Currently, my script only adds one player to the list(me, same happens to a friend who tried it for me). I’ve tried using .PlayerAdded but to no avail. Here’s the code I currently have in place:

local gangPart = game:GetService('Workspace'):WaitForChild('gangPart')
local Players = game:GetService("Players")
local PlayerGui = Players.LocalPlayer:WaitForChild("PlayerGui")

local GangGui = script.Parent
local PlayerList = GangGui:WaitForChild("PlayerList")
local PlayerTemplate = PlayerList:WaitForChild("PlayerTemplate")
local InviteBox = GangGui:WaitForChild("InviteBox")
local Notifications = GangGui:WaitForChild("Notifications")



function setPlayerImage(player, PlayerImage)

    local userId = player.UserId
    local thumbType = Enum.ThumbnailType.HeadShot
    local thumbSize = Enum.ThumbnailSize.Size48x48
    local content, isReady = Players:GetUserThumbnailAsync(userId, thumbType, thumbSize)

    PlayerImage.Image = content

end


function gangPartClicked(player)
    print(player.Name.." clicked the button")
end

print("line 32")

local function playerAdded(player)
    local timesLooped = 0


        local cloneTemplate = PlayerTemplate:Clone()
        cloneTemplate.Name = "clonedTemplate_".. player.Name
        cloneTemplate.PlayerName.Text = player.Name
        cloneTemplate.PlayerDisplayName.Text = player.DisplayName
        cloneTemplate.Visible = true

        cloneTemplate.Parent = player.PlayerGui.GangGui.PlayerList
        if timesLooped >= 1 then 
            cloneTemplate.Position += UDim2.fromScale(0, 0.11)
        else
            PlayerTemplate.Visible = false
            print("only one player")
        end

        setPlayerImage(player, cloneTemplate.PlayerImage)


        local cloneInviteBox = InviteBox:Clone()
        cloneInviteBox.Name = "clonedInviteBox_"..player.Name
        cloneInviteBox.Parent = GangGui

        if timesLooped >= 1 then
            cloneInviteBox.Position += UDim2.fromScale(0, 0.11)

        end

        cloneTemplate.PlayerImage.MouseButton1Click:Connect(function()
            cloneInviteBox.Visible = not cloneInviteBox.Visible
            print("image clicked")
        end)

        timesLooped += 1
        print("Gui successfully loaded in")

end

--Players.PlayerAdded:Connect(loadPlayers)

for _, player in ipairs(Players:GetPlayers()) do
    playerAdded(player)
end

print("line 63")

gangPart.ClickDetector.MouseClick:Connect(gangPartClicked)

Answer this question