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

How 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 player.Chatted functions in the script and they function as expected, until I combine them with the update method.

Working code before being cloned and updated:

function onChatted10(message, player)
    if message == "/ClearStorageBin" then
		ClearStorageBin() --Function that has been defined in another part of the script.
	end
end	
game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted10(message, player) end)
end)
function onChatted11(message, player)
    if message == "/StopRain" then
		StopRain() --Function that has been defined in another part of the script.
	end
end	
game.Players.PlayerAdded:connect(function(player)
    player.Chatted:connect(function(message) onChatted11(message, player) 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. All of that takes place when a server is started if I'm not mistaken.

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.

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 answer

You don't need to use an individual function for each chat command you want to have, you can instead have them in one function. Take this for example:

local function onChatted(player, message)
	if message == "/help" then
		-- Display help
	elseif message == "/cmds" then
		-- Display commands
	elseif message == "/reset" then
		-- Reset player
	end
end

Now to solve your problem, you just need to make sure that you declare the Chatted events for all players currently in the game when the script is created, instead of only for players that are added:

local function onChatted() 
	-- Defined as you wish
end

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

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

4 comments

0

Thanks for the single function info! I've implemented it already! Unfortunately I haven't been able to get the game to respond to the first player, even after trying the above. I copy and pasted it and ordered it exactly as outlined above without success. One thing I did notice in the output window was that it was saying "PlayerAdded is not a valid member of DataModel" (corresponding line 11 in yo

0

...(corresponding line 11 in the example) . I corrected that and I added a ")" to satisfy the output window but there was still no luck on my end. Any ideas what could be up?

0

Whoops, a little typo there. Should be game.Players.PlayerAdded

0

Haha, that is understandable. I forgot to mention that even after applying that fix, I was still having no success. I can see the logic behind why it should work the way you explained, but it doesn't seem to work on my end.

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