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

How do I fire the client when A character is added?

local Players = game:GetService("Players")

local function onCharacterAdded(character) print("CharacterAdded") game.ReplicatedStorage.PlayerJoinedMainMenu:FireClient() end

local function onPlayerAdded(player) player.CharacterAdded:Connect(onCharacterAdded) game.ReplicatedStorage.PlayerJoinedMainMenu:FireClient(player) end

Players.PlayerAdded:Connect(onPlayerAdded)

I can get the Client to fire when the Player joins with PlayerAdded but not when the Player respawns Using CharacterAdded my issue with CharacterAdded is that I need to reference the Player to fire it to But I don't know how to reference the Player on CharacterAdded.

How can I reference the Player on CharacterAdded?

1 answer

✓ Accepted answer

You can think of scope as a "block" of your code where variables live. The way you have your events set up, every event listener is in its own scope, but if you put the event listeners in the same scope, the CharacterAdded event listener will have access to both the player and character variables

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
	-- here we have access to the player variable
	game.ReplicatedStorage.PlayerJoinedMainMenu:FireClient(player)

	player.CharacterAdded:Connect(function(character)
		-- here we have access to the player AND character variables
		game.ReplicatedStorage.PlayerJoinedMainMenu:FireClient(player) 
	end)
end)
-- here we don't have access to either of the variables, because we are outside of the scope (due to the "end") 

1 comment

0

Thanks

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