CharacterAdded even wont run when the character first joins the game?

Hey! Im trying to make my Player with checkpoint data spawn AT their checkpoint using a player.CharacterAdded event. Although, it only will run AFTER the user joins. Meaning, when they first join the game, my script won’t run. Is this normal for a character added event? I’ve tried adding waits to see if the code is running too fast but that doesn’t seem to work. If someone can find a way to make the code run right when the player joins the game, please let me know!

player.CharacterAdded:Connect(function(character)

		local Humanoid = character:WaitForChild("Humanoid")
		local Torso = character:WaitForChild("HumanoidRootPart")

		wait(0.5)

		if Torso and Humanoid then
			if player.leaderstats.Stage.Value ~=0 then
				local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
				Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)		
			end
		end	
	end)

The event is inside a player added event.

Before CharacterAdded you need game.Players.PlayerAdded

I already have that, I was just showing my code for the character added event.

The most probable issue is that the client somehow went before the server? I don’t know if that’s the truth. Make sure that Character.Parent exists, because sometimes it hasn’t been parented into the game.

If that doesn’t work, you might have yielded something before the event was even connected. Otherwise, could you provide the entire code before the function?

Where do you make the Stage variable?

What do you mean by that? I made it right under the stage.Value if?

I think a way you could is wait for the character to exist the first time, then run the code you want to run and then set up the characteradded evnet?

game.Players.PlayerAdded:Connect(function(player)
	
	local function setPosition(char)
		local Humanoid = char:WaitForChild("Humanoid")
		local Torso = char:WaitForChild("HumanoidRootPart")

		wait(0.5)

		if Torso and Humanoid then
			if player.leaderstats.Stage.Value ~= 0 then
				local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
				Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)		
			end
		end	
	end
	
	player.CharacterAdded:Wait()
	
	setPosition(player.Character)
	player.CharacterAdded:Connect(setPosition)
end)

Not sure if this’ll do anything

I mean this i didn’t see any variable named “Stage”

player.CharacterAdded:Connect(function(character)

	local Humanoid = character:WaitForChild("Humanoid")
	local Torso = character:WaitForChild("HumanoidRootPart")

	wait(0.5)

	if Torso and Humanoid then
		if player.leaderstats.Stage.Value ~=0 then
			local StagePart = workspace.Stages:FindFirstChild(player.leaderstats.Stage.Value)
			Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)		
		end
	end	
end)

@StrafeDown this should work.

Hm, I tried this, it worked the first time then it doesnt. Its like a 50/50 chance. Weird.

May we see the full code to see if there’s anything yielding the script a lot before you get the character?

Players.PlayerAdded:Connect(function(player)

local Stats = Instance.new("Folder")
Stats.Name = "leaderstats"
Stats.Parent = player

local Stage = Instance.new("NumberValue")
Stage.Name = "Stage"
Stage.Parent = Stats
Stage.Value = 0

local Data = SaveDataStore:GetAsync(player.UserId)

if Data then

	print(Data.Stage)

	for i,stats in pairs(Stats:GetChildren()) do

		stats.Value = Data[stats.Name]		
	end			
else		
	print(player.Name .. " has no data.")			
end

player.CharacterAdded:Connect(function(character)

	local Humanoid = character:WaitForChild("Humanoid")
	local Torso = character:WaitForChild("HumanoidRootPart")

	wait(0.5)

	if Torso and Humanoid then
		if player.leaderstats.Stage.Value ~=0 then
			local StagePart = workspace.Stages:FindFirstChild(Stage.Value)
			Torso.CFrame = StagePart.CFrame + Vector3.new(0,1,0)		
	end
	end	
end)

end)

I believe you should reorder this code to after CharacterAdded event? It doesn’t quite work due to GetAsync is yielding.

1 Like

This seems to have worked. Interesting the getasync was yielding long enough for it not to work. Thanks!

1 Like