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

is the PlayerAdded function broken in LocalScripts?

I've done this in previous projects in studio and in game and it worked perfectly but it seems broken now...

game.Players.PlayerAdded:Connect(function(player)
	local clone = game.ReplicatedStorage.Template:Clone()
	local img = clone.ImageLabel
	local txt = clone.TextLabel
	local btn = clone.TextButton
	
	img.Visible = true
	txt.Visible = true
	btn.Visible = true
	
	clone.Parent = script.Parent
end)

1 answer

What you're doing:

Using a LocalScript( LocalScripts run if they're a descendant of the player), but using the PlayerAdded event anyway. Basically, you're listening for the event after the player has joined. That won't work because the player has already joined.

Solution:

Since LocalScripts already run if inside a player, you can just run the code directly without listening for any event.

Script:


    local clone = game.ReplicatedStorage.Template:Clone()
    local img = clone.ImageLabel
    local txt = clone.TextLabel
    local btn = clone.TextButton

    img.Visible = true
    txt.Visible = true
    btn.Visible = true

    clone.Parent = script.Parent

tl;dr = It isn't broken, it probably won't work because Experimental Mode is off, and even if it did work, you shouldn't use it.

If this doesn't work, send me an image of the player's hierarchy and i'll try to fix the script!

3 comments

0

What I was attempting to make was part of a trading system where it would show and remove players in the list as they joined and left but maybe I'm over complicating things.

0

Well, if you want to do that, you should make a function that loops through all the players and reloads the list, then when PlayerRemoving is fired, run the reload function.

0

PS: Run the function after the list is made, re run it when PlayerAdded is fired, since PlayerAdded is only for players who join after you.

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