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

Workaround to connect a PlayerAdded function after the fact?

I recently implemented an Auto-Update feature for a script that I've been developing. There are 14 functions that rely on the player.Chatted event in the script and they function as expected until I combine them with the update method.

Working code before being cloned and updated:

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted(message, player) end)
end)

function onChatted(message, player)
    if message == "/Mute" then
		MuteAll()
    elseif message == "/UnMute" then
		UnMute()
    elseif message == "/ClearStorageBin" then
		ClearStorageBin()
    elseif message == "/StopRain" then
		StopRain()
	end
end

game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message)
        onChatted(player, message)
    end)
end)

The method of updating I'm using is the InsertService to get the newest version of the script and then calling :Destroy() on the outdated script.

The problem arises when the chat functions no longer trigger like they did. My best guess is that they would still work for any player who joins after the first player because the new script will have had time to listen for them using: game.Players.PlayerAdded:connect(function(player) whereas the first player was noticed by the first script but that script has since been destroyed and now the first player is not recognized as having joined.

I have also tried:

for _,player in pairs(Players:GetPlayers()) do
 	player.Chatted:connect(function(message)     
		onChatted(player, message)
	end)
end

and:

for _, player in next, game.Players:GetPlayers() do
    player.Chatted:connect(function(message) onChatted(player, message)
    end)
end

in conjunction with the game.Players.PlayerAdded:connect(function(player)bit.

Is there a workaround to make sure that the first player can use the chat commands as well? Applying your ideas to the specific references above would be immensely helpful for me as well.

Thanks!

1 comment

0

Any players added after the code is run won't be affected.

1 answer

✓ Accepted answer

This is why I dislike anonymous functions for events -- the function usually does a real concrete action, not just handle an event. It makes sense to name it accurately and make it visible elsewhere.


You already have a function that "sets up" a player in your game, which is this:

function(player)
    player.Chatted:connect(function(message)
        onChatted(player, message)
    end)
end

So give it a name!

function setupPlayer(player)
    player.Chatted:connect(function(message)
        onChatted(player, message)
    end)
end

Now you just need to make sure every player that joins from here-on-out gets set up:

game.Players.PlayerAdded:connect(setupPlayer)

and everyone who already was there gets set up:

for _, player in pairs(game.Players:GetPlayers()) do
	setupPlayer(player)
end

1 comment

0

This is a great answer. Thank you.

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