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

How can I make this function fire every time a player joins? [closed]

Asked by
aindev 4
6 years ago
Edited 6 years ago

This question already has an answer here:

How can I do something to do something when a player joins?

I am making a custom leaderboard on my game. Please help. If I do this, it will just keep repeating my name.

local frame = script.Parent


function update()
    for _,player in pairs(game.Players:GetChildren()) do
        local text = game.ServerStorage.TextLabel:Clone()
        text.Text = player.Name
        text.Parent = frame
    end
end

while true do
update()
wait(1)
end
0
Duplicate of https://scriptinghelpers.org/questions/7018/how-can-i-do-something-to-do-something-when-a-player-joins. Please make sure you search up your problem before asking. RayCurse 1518 — 6y

Marked as Duplicate by RayCurse, shayner32, green271, valchip, and evaera

This question has been asked before, and already has an answer. If those answers do not fully address your question, then please ask a new question here.

Why was this question closed?

3 answers

Log in to vote
0
Answered by
shayner32 478 Trusted Moderation Voter
6 years ago

It's quite simple using the code you currently have. What you are looking for is the event in the Players service named PlayerAdded, which fires every time a player is in the process of joining the game.

Using your existing function, making it run whenever someone joins is pretty simple:

local frame = script.Parent


function update(player)
    local text = game:GetService("ServerStorage"):WaitForChild("TextLabel"):Clone()
    text.Text = player.Name
    text.Parent = frame
end

game:GetService("Players").PlayerAdded:Connect(update)

It's important to use GetService() when referencing any service, as the service may not exist at the time the script runs, may be named differently, etc.

In addition, it's important to use WaitForChild() when getting the TextLabel. When a new server is created, the PlayerAdded event may be fired for the first player before all parts of the game fully load in, and thus the TextLabel might be missing (so you should use WaitForChild).

Ad
Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

You can use the PlayerAdded event which fires when a player joins, but it only can work on server scripts. You can use the parameter of the event to get the player that joined like this example:

game.Players.PlayerAdded:Connect(function(player) -- event of the Players service
    print(player.Name) -- prints the player's name
end)

Also theres another event called PlayerRemoving which fires when a player leaves and also has the player parameter and will only work in the server. I cant give you a updated version of the script since PlayerAdded only works on the server so you need to use Remote events to let the server communicate with the client to change your GUI.

Log in to vote
-1
Answered by 6 years ago
Edited 6 years ago

The whole "repeat until" thing is just so the first person who joins the game will be added instead of only the second, third, fourth, fifth, and so on. Also I made it so if a player leaves, they are removed from the list, enjoy! :D

-- Adding a player to your list
local frame = script.Parent
game.Players.PlayerAdded:Connect(function(player)
    if not script.Parent:FindFirstChild(player.Name) then
        local text = game.ServerStorage.TextLabel:Clone()
        text.Text = player.Name
        text.Parent = frame
    end
end)

-- Removing a player from your list
game.Players.PlayerRemoving:Connect(function(player)
    if script.Parent:FindFirstChild(player.Name) then
        script.Parent:FindFirstChild(player.Name):Destroy()
    end
end)

-- Adding the first player to join the game (The first person's name won't be in the list without this)
repeat
    wait()
until #game.Players:GetChildren() >= 1

for i=1, #game.Players:GetChildren() do
    local children = game.Players:GetChildren()
    player = children[i]
    if not script.Parent:FindFirstChild(player.Name) then
        local text = game.ServerStorage.TextLabel:Clone()
        text.Text = player.Name
        text.Parent = frame
    end
end