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

How would you retrieve a players name?

Asked by 8 years ago

okay, so i coded this GUI that creates a list of clickable buttons, each with a players name on them.

function Nominate()
    local players = game.Players:GetChildren()
    local template = game.Lighting.Template
    local num = 0.2
    for i = 1, #players do
        templateclone = template:clone()
        templateclone.Parent = game.Players.Player.PlayerGui.NominationGUI
        templateclone.Position = UDim2.new(0.5, -100, num, 0)
        --{0.5, -100},{0.2, 0}
        --{0, 0},{-50, 0}
        templateclone.Text = players[i].Name
        num = num -0.1
    end
end

So, i want to know is how do you retrieve the name of the player you clicked?

0
? Ethan_Waike 156 — 8y

1 answer

Log in to vote
0
Answered by 8 years ago

So, not only will I answer your question but I will bring it up a notch:

Player=game.Players.LocalPlayer
PGui=Player:WaitForChild("PlayerGui")
NomGui=PGui:WaitForChild("NominationGUI")

function PS(st) --Person Selected
    if game.Players:FindFirstChild(st) then
        local player=game.Players[st]
        print("You selected "..player.Name
        --Stuff you want to do
    end
end

function Nominate()
    local template = game.Lighting.Template
    local num = 0.2
    for i,v in pairs(game.Players:GetChildren()) do --Easy way to loop through children
        templateclone = template:clone()
        templateclone.Parent = NomGui
        templateclone.Position = UDim2.new(0.5, -100, num, 0)
        --{0.5, -100},{0.2, 0}
        --{0, 0},{-50, 0}
        templateclone.Text = v.Name
    templatecone.Name="Person"
    templateclone.MouseButton1Click:connect(function() --Create click function for each template
        PS(template.Text)
    end)
        num = num -0.1
    end
end

game.Players:PlayerAdded:connect(function(player) 
    for i,v in pairs (NomGui:GetChildren()) do
        if v.Name=="Person" then
            v:Destroy()
        end
    end
    Nominate()
end)

game.Players.PlayerRemoving:connect(function(player)
    for i,v in pairs (NomGui:GetChildren()) do
        if v.Name=="Person" then
            v:Destroy()
        end
    end
    Nominate()
end)

--The last two functions will update the Gui when a player enters or leaves

Remember, this is set up for a local script but if you have the game FE, that will lead you into a bit of trouble if you want to change a value or that player. But you can always use a RemoteFunction or a RemoteEvent.

Ad

Answer this question