Character added not working

Character added is not working. I am using a starter character which is just a roblox r15 blocky rig for testing. I don’t know why. No other scripts in game interact with player.

-- services
local Players = game:GetService("Players")

-- variables
local CharacterAlive = true
local Player = Players.LocalPlayer
local Head, Neck

Player.CharacterAdded:Connect(function(character)
	Head = character:WaitForChild("Head")
	Neck = Head.Neck
end)
print("plyh2")
-- functions
while CharacterAlive do
	print("Pluh")
	task.wait(0.1)
end	




-- events
Player.CharacterAdded:Connect(function() 
	print("hello")
end)
Player.CharacterAdded:Connect(function()
	CharacterAlive = true
end)
Player.CharacterRemoving:Connect(function()
	CharacterAlive = false
end)
2 Likes

Perhaps the character added event is being fired before you set the listener for the event. Do a check before setting the listener to see if the character already exists.

1 Like

Why are you using multiple CharacterAdded events in different places? Just put the code in a singular one. Maybe that’s what’s causing your problem.

1 Like

I was testing prints and removed from the one. I use character removing too

1 Like

I print the player.character and nothing printed. The other stuff prints and it is in a local script with output context set to all.

1 Like

No, that’s not what I mean. You have 3 CharacterAdded events connected after one another. just take the code from the last two, and put it into the first one.

1 Like

They would all just fire, it doesn’t matter how many.

1 Like

Oh yeah but i also like readability

Your listeners at the bottom past the while loop will not be set while the while loop is active either btw.

2 Likes

You’re right. I found the cause of the problem. Aaand it’s from the our good friend, loops.

The reason nothing prints is because you’re connecting a while wait() do loop before some pretty important events, including the one that prints and the other one that switches alive to true. I would highly recommend putting the loop before any events in the future.

Also, you could put the loop in a task.spawn() function, and that way, you don’t have to move anything.

2 Likes

I forgot that loops pause stuff i usually just use render stepped and stuff…

2 Likes

Good to hear. I wouldn’t recommend scattering the same events throughout the script though. It’ll help in the future.

1 Like

@BackSpaceCraft is correct though. Its probably better to keep a single listener event. You can delegate other functions to fire from that to keep the code cleaner but multiple listeners is inefficient.

Still doesn’t work though, it prints nil when i print the character.

Show the updated code please so we can see if there is a problem with that specifically.

Where are In which event are you printing it in?

-- services
local Players = game:GetService("Players")

-- variables
local CharacterAlive = nil
local Player = Players.LocalPlayer
local Head, Neck


print("plyh2")
-- functions

task.spawn(function()
	while CharacterAlive do
		print("Pluh")
		task.wait(0.1)
	end	
end)

-- events
print(Player.Character)
Player.CharacterAdded:Connect(function(character)
	CharacterAlive = true
	Head = character:WaitForChild("Head")
	Neck = Head.Neck
end)
Player.CharacterRemoving:Connect(function()
	CharacterAlive = false
end)
1 Like

Are you doing it on server or local side

1 Like

Why… are you printing it before the characterAdded event?

1 Like

Im at school bruh idk what a doing

1 Like