CharacterAdded not working for the first time, even with a safety function

Hey! My CharacterAdded function is not working for the first time that you spawn, even though I made a specifical “safety” function for that.

local player = game.Players.LocalPlayer
local text = script.Parent:WaitForChild("EggsStatus"):WaitForChild("CollectedStatus")

if player.Character then return else repeat wait() until player.Character end

-- more code here

if p;ayer charcter then return?
if there is plahyercharacter then you finish the function? thats probably the issue, return

just do this

local character = player.Character or player.CharacterAdded:Wait()
3 Likes

Still doesn’t seem to work for some reason

The code you provided only continues past the if-statement if the player is not spawned in when the code is run. If we expand you if-statement for more readability it becomes more obvious:

if player.Character then
    return -- This stops any further code from running inside the function
else
    repeat
        wait()
    until player.Character
end

If the player is already spawned in when this if-statement is evaluated, you return out of the function, and no further code is executed.

Like @yoshicoolTV suggested, you should be making use of events as well, instead of repeatedly checking if the character is spawned in.

To fix your issue, remove the if-statement on line 4 of your codeblock, and replace it with the codesample @yoshicoolTV provided.

1 Like

I already did replace it with the code, however, it still doesn’t work.

There aren’t any other issues in the code you provided, so the issue would be somewhere else in your code. Could you please send a larger codesnippet?

Where exactly is that script located at

1 Like

Just do it like this:

local function onCharacterAdded(character: Model)
	--whatever you want done when character is added
end
  
player.CharacterAdded:Connect(onCharacterAdded)
if player.Character then
	onCharacterAdded(player.Character)
end

Remove this though:

1 Like

I can send you the ServerScript (RemoteEvents and overall server-side handling) as well, if you need it.

X
1 Like

The LocalScript is located inside of a ScreenGui. The ServerScript (for the events, not the characterAdded function) is located inside of a hitReg part in Workspace.

I would suggest not having scripts inside specific parts in workspace, instead make it modular.

You also do not need to do most of the things that has kindly been suggested in this thread.

The issue is not that character isn’t existing, so you do not have to check after if character is a thing in the characteradded event.

You say that CharacterAdded is not working. What exactly is not working? Have you tried to make prints in certain stages of the script? (before any if-statemen for example)

This is how to connect a characteradded event correctly in a localscript

local localplayer = game:GetService("Players").LocalPlayer

localplayer.CharacterAdded:Connect(function(character)--character is always first parameter in a characterAdded event, no need to check for it.
	-- do things in here.
end)

Your current script tries to wait for character to be a thing, before initializing the characteradded event, this is a incorrect way of defining character in your use-case. Instead character is defined as the first parameter in the characteradded event, which your script did not have. :slight_smile:

1 Like

Just tested it out with breakpoints and print statements. The breakpoint does work, even for the first time, however, the print statement does not. The print gets outprinted only on the second (or more) spawn, not on the first one. I’m not really sure why that happens though, seen that the breakpoint works.

The issue in your current script is that at the top of the script, you try to define character, you do not need to do this, since your characteradded event wont be initialized before after the character has spawned. Also make sure to turn off, so the GUI does not reset on death.

1 Like

Ye, ResetOnSpawn was turned off already. I also just removed the line, as you suggested. However, it still doesn’t work. The issue with the outprinting (and the gui overall) persists.

Send the current script again so I can take a closer look.

LocalScript

X

ServerScript (didn’t really touch that rn)

X

They’re quite bigger scripts and maybe hard to read for you (not for me), sorry about that if so.

Oh did not even need the script, you see you wait for a lot of things to spawn in before initializing the characteradded event, so same issue, character has already spawned in.

Put the localscript inside StarterPlayerScript instead, and then adapt it to work even when waiting for objects to spawn. This is a simplified version:

local localplayer = game:GetService("Players").LocalPlayer

task.wait(5) -- This is only there do demonstrate that this will work, even after 5 seconds.

local function DoSomethingFunction(character)
      print(character.Name.. " hurray!")
end
localplayer.CharacterAdded:Connect(function(character)-- do this if character has not already spawned
	DoSomethingFunction(character)
end)
-- Since we have no wait between our characteradded event and the lines below,
-- it shouldn't run DoSomethingFunction more than once when initializing for the very first time.
-- After intiializing the characteradded event, we run DoSomethingFunction
local character = player.Character
if character then -- do this if character has already spawned
	DoSomethingFunction(character)
end

When that is said, I cannot look through all the code and pick out all the flaws. I’ve given you a good baseline above to how you could do it. :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.