Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How can I make this function fire every time 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

1 comment

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.

3 answers

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).

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.

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

This discussion is preserved as it was. New answers, comments, and votes are closed.