CharacterAdded:Wait() continues to yield even after the character has been added

This is a problem I’ve been having for the last couple of days. If ‘CharacterAdded:Wait()’ is in the script, even if the character is already loaded in, it continues to yield. There are no errors what so ever. An example would be this local script:

local player = game.Players.LocalPlayer
print(player)
player.CharacterAdded:Wait()
print(player.." 2")
--local event = game.ReplicatedStorage.RemoteEvent
--print(event)

while true do
	wait(0.5)
	print("i-")
	workspace["part 1"].Position = Vector3.new(math.random(-999, 999), 500, math.random(-999, 999))
	workspace["part 2"].Position = Vector3.new(math.random(-999, 999), 500, math.random(-999, 999))

	local LightningBolt = require(game.ReplicatedStorage.LightningBolt)
	local LightningSparks = require(game.ReplicatedStorage.LightningBolt.LightningSparks)
	--Create a new bolt with 40 parts
	local NewBolt = LightningBolt.new(workspace["part 1"].Attachment, workspace["part 2"].Attachment, 40)
	--Then, update properties to your liking
	NewBolt.CurveSize0, NewBolt.CurveSize1 = 10, 15
	NewBolt.PulseSpeed = 2
	NewBolt.PulseLength = 0.5
	NewBolt.FadeLength = 0.25
	NewBolt.MaxRadius = 1
	NewBolt.Color = Color3.new(math.random(), math.random(), math.random())
	local NewSparks = LightningSparks.new(NewBolt)
end

I know that it continues to yield because, in the output, it doesn’t print my ‘O_Ogosh1 2’, which is what I told it to do after the character was loaded.

https://streamable.com/bu4pq5

I have already looked in the developer Hub, but haven’t found anything that pertains to this, if you have a solution or saw a post that had a similar problem please do tell.

1 Like

Replace this:

For this:

Character = player.Character or player.CharacterAdded:Wait()
print(player.Name,"2")

It doesn’t matter if you use it or not.

1 Like

Thank you very much. It works now! But, do you know why making a variable instead causes it to work properly? I just wanted to know in case I come across a similar problem again.

If the character already loaded, it will wait for it to load again. The variable gets the character if it already loaded instead of waiting

2 Likes

Exactly, Roblox thinks it’s a function.

Oh ok, thank you for explaining that.

It isn’t really that you need a variable for the character, it’s that you don’t need to wait for the character to spawn if it already exists.

This also works:

if (not player.Character) then
	player.CharacterAdded:Wait()
end
1 Like

Thank you for the feedback, I appreciate it.