Character Replication Problem

I did the Character Switch System and it worked earlier.

The problem is that the scripts gets broken because of character.

I tried to somehow fix this, no results.

Server switches player’s character to new one, and it did successfully. The new character gets added, old character gets deleted, even Properties says on client/server that character has been changed. But the client scripts don’t understand that, they receive old character instead of new one.

Here’s the code, that runs on server.

function module:SpawnCharacter(player: Player, newCharacter: Model)
	newCharacter:PivotTo(player.Character:GetPivot())
	newCharacter.Parent = workspace

	player.Character = newCharacter
	HitboxCollisionManager:AddHitbox(player.Character)
	charRespawnE:FireClient(player)
	
	setupCharacter(player.Character)

	print("Player", player.Name, "spawned as:", newCharacter:GetAttribute("name"))
end

And here’s for client.

charRespawnE.OnClientEvent:Connect(function()
	local humanoid = player.Character:FindFirstChildOfClass("Humanoid") :: Humanoid
	camera.CameraSubject = humanoid
	print(player.Character) -- prints old player character
	ControllerModule.new()
end)

You are firing the remote before a function which you didn’t share, could be some issue in that function, sharing that function could help. Also if the sever has the correct new character try passing that to to the client and using that.

Would it work if you added a small delay? It could just be that the event reaches the client before the property replication does. I can’t figure out anything else that’s wrong with this…

function does additional stuff not related to the issue.
i can share it anyways.

function setupCharacter(character: Model)
	local player = Players:GetPlayerFromCharacter(character) :: Player
	local humanoid = character:FindFirstChildOfClass("Humanoid") :: Humanoid
	humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None

	Ragdoll.new(character)
	
	humanoid.Died:Once(function()
		Ragdoll:MakeRagdollClone(character, true, 25)
	end)
	
	player.CharacterRemoving:Once(function()
		player:FindFirstChild("PlayerTeam").Value = "Neutral"
	end)
end

Hello, you can add a wait loop inside the connected function on the client

repeat wait() until game.Players.LocalPlayer:GetPropertyChangedSignal("Character"):Wait()
-- print

See if the new character prints.

Applying delay to remote fire fixed the problem, thanks. But now, i’m thinking how to do it properly

This should work, it adds a delay until the new character is found.

I don’t think there is a way to do it properly other than the delay. You could maybe wait for a descendant to enter the workspace which has a tag unique to the character model, and then check if it’s equal to the player’s character property (or just use an attribute which is set to the player’s user ID so you don’t have to wait for the property)

Okay, thanks for helping, i’ll mark a solution, see ya.