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

Gui on PlayerAdded doesnt work!?

So i wanted to make a script when someone joins the game a GUI is being exposed to everyone in the game saying the joined players username. But instead, i get when a player joins the game it only shows it on joined player's screen and not anyone elses. Help!

game.Players.PlayerAdded:connect(function()
	for i, v in ipairs(game.Players:GetChildren())do
		wait(1)
		v.PlayerGui.GreenieEntered.Frame.Visible = true
		v.PlayerGui.GreenieEntered.Frame.TextLabel.Text = "A Greenie".."("..v.Name..")".."has joined The Glade!"
		wait(8)
		v.PlayerGui.GreenieEntered.Frame.Visible = false
		end
	end)

-- :(

Please help!

2 answers

This won't act how you would want.

  1. Set first player's message

  2. Clear first player's message

  3. Set second player's message

  4. Clear second player's message

etc.

Also, you're reporting v.Name instead of the new player's name (you didn't give the PlayerAdded event a parameter, so this didn't show)

In addition, you should be using robust error-checking.


We want to change it to

  1. Set all player's message

  2. wait

  3. Clear all player's message

function enterLabel(player)
	return player:FindFirstChild("PlayerGui")
		and player.PlayerGui:FindFirstChild("GreenieEntered")
		and player.PlayerGui.GreenieEntered:FindFirstChild("Frame")
		and player.PlayerGui.GreenieEntered.Frame:FindFirstChild("TextLabel")
end

function newPlayer(player)
	wait(1)
	local message = "A Greenie (" .. player.Name .. ") has joined The Glade!"
	for _, player in pairs(game.Players:GetPlayers()) do
		local label = enterLabel(player)
		if label then
			-- Has the necessary GUI objects
			label.Text = message -- Show message
			label.Visible = true
		end
	end
	wait(8)
	for _, player in pairs(game.Players:GetPlayers()) do
		local label = enterLabel(player)
		if label and label.Text == message then
			-- If someone else joined, we don't want the first join message
			-- to clear the second.
			label.Text = ""
			label.Visible = false
		end
	end
end

game.Players.ChildAdded:connect( newPlayer )

3 comments

0

Really, Blue? xD

0

I was already typing it! I swear!

0

Ok, I believe you. xD

It's because of the waits. Every single player, it's waiting a total of 9 seconds.

Try this instead:

game.Players.PlayerAdded:connect(function(player) -- Also get the player.
	for i, v in pairs(game.Players:GetPlayers())do -- Use pairs instead of ipairs. pairs is more reliable. Sometimes in rare occasions, ipairs won't go through all players.
		spawn(function() -- Starts a coroutine. In other words, it doesn't pause the current routine.
			local frame = v:WaitForChild("PlayerGui"):WaitForChild("GreenieEntered"):WaitForChild("Frame") -- Just makes sure everything is there. Due to the 'spawn' function above, it won't pause the code between players.
			-- Setting all this to 'frame' makes the code easier to read in the next few lines.
			frame.Visible = true
			frame:WaitForChild("TextLabel").Text = "A Greenie".."("..player.Name..")".."has joined The Glade!"
			wait(8) -- Again, doesn't pause the code.
			frame.Visible = false
		end
	end
end)

1 comment

0

Thanks!

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