Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago
Edited 3 years ago

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

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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") 
0
Thanks AidanGrumpygammer 2 — 3y
Ad

Answer this question