Quick question about a CharacterAdded Event?

Hey everyone. So, i am trying to fire a Character added event in a module script that functions locally. It works, but not at first… it doesn’t work when you first enter the game, but when you reset it works. Does anyone know what’s going on with that?

I tried also putting a player added event before it, but still nothing.

My guess is that when you’re playtesting it your character has already loaded in the game before the module script connected to the event. Try instead running a local server so the client joins a bit later.

Yeah i’m testing it, so that shouldn’t happen in game play then?

Correct, since in game play the clients join later but in play solo it joins almost immediately. What you could also do is move the place where you require the module and the event connector to the top to try and have it run faster than playsolo.

Well, i wasn’t playing in play solo, i did create a local server, with two players to suit my purposes. :confused: I’m going to try playing in game aswell

How long does it take for you to connect the event to the function?

That probably means your character is being added AFTER the event is initialized. Create a function that you want to run in your character added event and run that function as soon as the script initializes.

if Player.Character then
CharacterAdded(Player, Player.Character)
end

Something like this could help you.

Hey guys, @azahid1 @kinkocat i’m still having this issue, and the solutions you posed have not helped. I even have tried repeat wait() until player.Character aswell as repeat wait() workspace:FindFirstChild(player.Name)

At the beginning, when the game is loaded, the event connection can be loaded after the first character is added, so it won’t detect it. To solve the problem I do something like that:

function a(character) end -- function that is connected to the event

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(a); -- here is the connection that you have
end)

-- and you have to add something like that
for i,v in pairs(game.Players:GetChildren()) do
   if v.Character then
       a(v.Character); -- here execute the function a
   end
end

So if a character had been added before the script was loaded and connections initialized, the code at the end will run and execute the function for all player characters in the game.

And I want to point out that you will not execute anything twice, because all the characters that will be affected by the code at the end, were not affected with the CharacterAdded event connection.

If you’re working locally, not on the server, simply do

local plr = game.Players.LocalPlayer;
local char = plr.Character;
if char then
    a(char);
end

at the end of the code instead of the loop from the first code.

You can try something like this:

local function CharacterAdded(character)
-- code here
end

if player.Character then
    CharacterAdded(player.Character)
end

player.CharacterAdded:Connect(CharacterAdded)

If you are familiar with the Aero game framework, i have the event at the :Start() of the module which runs as soon as the game starts. And thats where i have the event(and it is local)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        if player.Character then
             Inventory_Controller:Inventory_GUI_Stuff()
        end
     end)
end)

i even did

game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
    repeat wait() until player.Character
    if player.Character then
         Inventory_Controller:Inventory_GUI_Stuff()
    end
 end)

end)

Might I suggest utilizing:

Player.CharacterAdded:Wait() instead of repeat wait() until Player.Character?

In my own experience, using repeat wait() is generally slower performing than the former.

You can read up for more information at:

1 Like

I think you didn’t read my post, try it to see if it works, using repeat wait () is not a good way to do it and it may not work much of the time and even cause more problems.

I hope I can solve it here. Alright so since your module is placed on the client, this means that things load in at differing times. The reason why your module script didn’t recognize the initial CharacterAdded event is because it ran too slow (after the initial character was spawned). It’s not much of an issue but if you want to get the most recent Character in workspace you can simply do the following:

local Character = Player.Character and Player.Character.Parent and Player.Character or Player.CharacterAdded:Wait()

Hope this helped :smile:!

He want’s the most recent character in workspace, you do realize that right?

It doesn’t do the same thing as mine. Do you understand how the character re spawning works in Roblox?

You want it to yield the thread … that’s the best way to get the most current character in worksapce.

If you are working with Aero Game Framework (I do quite frequently as well!), something that could be happening is that your event is firing late. That’s been pointed out a few times I know but it’s good to try creating a catcher function for these cases.

Considering the OP says that it’s a module that works locally, I’m going to assume that you’re making a controller. In this circumstance, you don’t need to do any PlayerAdded. Hook everything straight to the character event: you can.

function Controller:Start()
    local LocalPlayer = game:GetService("Players").LocalPlayer

    local function onCharacterAdded(character)
        Inventory_Controller:Inventory_GUI_Stuff()
    end

    LocalPlayer.CharacterAdded:Connect(onCharacterAdded)

    if LocalPlayer.Character then
        onCharacterAdded()
    end
end

This should be what you’re looking for. If it’s not, mind providing me some details so I could attempt to repro the problem? I work a lot with Aero and don’t seem to have troubles with CharacterAdded or anything.

1 Like

Ah ok… ill give this a try, would putting it in the Controller:Init() do the trick? @colbert2677

This worked thanks a lot! :slight_smile: This whole time i think i just needed someone who understood this framework better than I

I’d leave it in the Start method. Init is for setting up your module’s resources if you need any during the code’s lifetime. This also includes access to other Aero modules (since modules are loaded first before services and controllers are initialised and started).

2 Likes