Player.CharacterAdded() Firing before player spawns?

I’ve been working on my Bumper Cars game, and I’ve run into an issue which causes certain players to spawn above the map.

I have parts in each map called ‘CarSpawnPoint’. Each player has a variable set for which map they will spawn to (Premium members may choose), and when the car spawns, it teleports its chassis 2 studs above the point (to allow space for the wheels) with SetPrimaryPartCFrame. If the variable is not set, the car will go to 0, 0, 0, which is the position of the City map’s spawn point (meaning the variable is not nil, or the players would be spawning in the map, not above it). Script errors are logged in my Discord server, none of which relate to this problem. But for certain players (not all), the function seems to be running before the player fully spawns, causing the car to teleport to the default spawn position (which I’d estimate to be about 0, 50, 0). For these players, it happens every time.
Here’s a quick visual :laughing::

The car spawns via the Car.new function, which is stored in a ModuleScript and runs on Player.CharacterAdded():

Car.new = function(player, carType, mapType)
	local newCar
	if carType then
		newCar = SourceCars[carType]:Clone()
	else
		newCar = SourceCars.Standard:Clone()
	end
	newCar.Name = player.Name.."'s Car"
	Engine.SetStatistics(player, newCar) -- These functions are just setting variables and creating welds, nothing which would contribute to this problem.
	Physics.weld(player)
	Physics.tweak(player)
	newCar.Parent = Cars
	newCar.PrimaryPart:SetNetworkOwner(player)
	player.Character.Humanoid.JumpPower = 0
	newCar:SetPrimaryPartCFrame(CFrame.new(Car.FindSpawnPoint(player, mapType)+Vector3.new(0, 2, 0)))
	newCar.Seat:Sit(player.Character.Humanoid) -- I've tried this both before and after the car's position is set; nothing changes
	return(newCar)
end

Show screenshots. I hardily tell what you’re trying to deal with by that one drawing picture.

Im really confused on what your problem is. I do see that nothing is attached to the player.CharacterAdded function but again. That might not even be close to your problem

Unfortunately, CharacterAdded firing before the character actually spawns is the result of the avatar loading events being in a screwy order. Roblox is currently working on an update that will fix this, however it has been deferred due to insufficient resources and focuses placed elsewhere. See here for more information on how the current events look and what it’ll be fixed to in the future:

There’s some code in RbxCharacterSounds that accounts for this. You can try looking through it for a reference of how to skirt around and ensure that when your code should start running, the character has been fully spawned in.

1 Like

I can’t show screenshots, this only happens to some players. Never happened to me.

Took a look at the code, and this is what I came up with:

repeat wait() until player.Character:FindFirstAncestor('Workspace') and player.Character:FindFirstChild('HumanoidRootPart')

Would this work? As I said before, it’s never happened to me so I can’t test it.

You can reference the initialisation code from RbxCharacterSounds here. You’ll notice that it’s quite a hefty function, so it takes quite a bit to fully ensure the character has spawned in. Out of 50 lines of code, only one line, 297, initialises the system, knowing that the character has properly spawned in.

That’s accounting for memory leaks as well and using an event-based approach over loops.

1 Like