[Help!] "CharacterAdded" sometimes doesn't work

I have a problem, my script is using the “CharacterAdded” however sometimes it works and sometimes it doesn’t and my whole game stops working. I would appreciate your help.

game.Players.PlayerAdded:Connect(function(Player)	
	local Stats_Folder = Player:WaitForChild("Stats")

	local Biceps_Value = Stats_Folder:WaitForChild("Biceps")
	local Torso_Value = Stats_Folder:WaitForChild("Torso")
	local Forearm_Value = Stats_Folder:WaitForChild("Forearm")
	local Legs_Value = Stats_Folder:WaitForChild("Legs")
	
	local GamePasses_Folder = Player:WaitForChild("GamePasses")
	local SteroidsPass = GamePasses_Folder:WaitForChild("Steroids")
	---the script freezes here
	Player.CharacterAdded:Connect(function(Character)
		
		local NewValueforMax = 50000

		if SteroidsPass.Value == true then
			NewValueforMax = 100000
		end
		
		repeat wait() until Character:FindFirstChild("Humanoid") and Character:FindFirstChild("LeftUpperArm") -- triying to fix
		task.wait(0.5)
5 Likes

First, check if the player’s character already exists. You can handle it immediately without relying on the event.

if Player.Character then
    -- Handle the existing character here
end

Instead of using a repeat loop with wait(), you can use a while loop and yield the thread with wait() inside it.

while not (Character:FindFirstChild("Humanoid") and Character:FindFirstChild("LeftUpperArm")) do
    wait()
end

It’s possible that the CharacterAdded event is firing after the player has left the game. You can check if the player is still in the game by using the Parent property of the player’s character.

Player.CharacterAdded:Connect(function(Character)
    -- Check if the player is still in the game
    if Player.Character.Parent ~= workspace then
        return
    end

    -- Rest of your code
end)

Hope this helps.

3 Likes

It still doesn’t work for me

I added this, but now the code no longer continues, sorry i use translator.

Player.CharacterAdded:Connect(function(Character)
    -- Check if the player is still in the game
    if Player.Character.Parent ~= workspace then
        return
    end
2 Likes

Try this instead.

if Player.Character ~= nil then
--Whatever you wanna do here.
end
2 Likes

I already tried, but the same thing keeps happening sometimes

3 Likes

Try replacing the connection at line 19 with a repeat until loop. So it should look like this.

repeat wait() until Player.Character ~= nil
		
local NewValueforMax = 50000

if SteroidsPass.Value == true then
	NewValueforMax = 100000
end
		
repeat wait() until Character:FindFirstChild("Humanoid") and Character:FindFirstChild("LeftUpperArm") -- triying to fix
task.wait(0.5)
--Whatever you want to do after this.
2 Likes

Why don’t you just use :WaitForChild()?

local Humanoid = Character:WaitForChild("Humanoid")
local LeftUpperArm = Character:WaitForChild("LeftUpperArm")
3 Likes


the same thing keeps happening

3 Likes

No. Replace the connection at line 19. Sorry, I accidentally said line 22 earlier. You can get rid of line 22 after that. Again, it should look like this:

--Whatever comes before this.
print("Solo llego aqui")
repeat wait() until Player.Character ~= nil --Line 19
print("Solo llego aqui eeeeeee")

local NewValueforMax = 170000

--Whatever comes after this.
2 Likes

@MichaelAharon123 Is it working now? If not, don’t hesitate to ask.

1 Like

Sorry for not answering, yes it works, but when the player respawns everything gets bugged, for that reason I want to use CharacterAdded

The issue lies in your yielding statements in getting the values needed. Because the code is yielding for these values, there is a possibility that the character has already spawned in, so CharacterAdded won’t fire. To get around this just add this statement that checks if the player’s character already exists.

game:GetService("Players").PlayerAdded:Connect(function(Player)
   -- your code
   if (Player.Character) then
      YourCharacterAddedFunction(Player.Character)
   end
   Player.CharacterAdded:Connect(YourCharacterAddedFunction)
end)
2 Likes

Instead of using a repeat wait() use Character:WaitForChild(“Humanoid”) and Character:WaitForChild(“LeftUpperArm”)

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