Swapping 2 rigs works fine, but after 1 dies and respawns, instead of changing its cframe to the desired cframe, it gets teleported to where it died

  1. I want two rigs/characters to swap positions when a function is invoked

  2. The code works fine, but whenever I reset and do it, instead of teleporting to the other rig’s position, I teleport to where I died.

  3. Ive recoded this same code 3-4 times, and tried every (?) method of moving the character but the same issue arrises.

function module.Swap(player, keystroke)

if FuncsMDL.manacheck(player, 20) == false then return end
local character = player.Character
local RootPart = character.HumanoidRootPart
local Hum = character.Humanoid
local animator = Hum:WaitForChild("Animator")

local track = Hum.Animator:LoadAnimation(game.ReplicatedStorage.Animations.Magic.Snap)
track:Play()

task.spawn(function()
	wait(0.3)
	FuncsMDL.sound(game.ReplicatedStorage.FX.Sounds.Magic.FingerSnap, character.RightHand, 2)
end)

miscscreen:FireClient(player, "GETCAMERADIR")
miscscreen.OnServerEvent:Connect(function(player2, pos, dir)
	if player2 ~= player then return end
	local params = RaycastParams.new()
	params.FilterDescendantsInstances = {character:GetChildren()}
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.IgnoreWater = true

	local raycast1 = workspace:Raycast(pos, dir * 500, params)
	if raycast1.Instance then
		local function createpart(hitted)
			local part = Instance.new("Part")
			part.Transparency = 0.5
			part.CanQuery = false
			part.CanCollide = false
			part.Anchored = true
			part.Parent = workspace
			part.CFrame = hitted.CFrame
			
			part.Size = hitted.Size
			part.Color = Color3.new(1, 0, 0)
			part.Material = Enum.Material.Neon
			
			game.Debris:AddItem(part, 10)
			
			return part
			
		end
		
		local hit = raycast1.Instance
		
		if hit.Anchored == false then
			if hit.Parent:IsA("Model") and hit.Parent.PrimaryPart ~= nil then
				hit = hit.Parent.PrimaryPart
			end
			
			local enemypart = createpart(hit)
			local playerpart = createpart(RootPart)
			
			character:SetPrimaryPartCFrame(enemypart.CFrame)
			hit.CFrame = playerpart.CFrame
		end
		
	end
end)

end

Before dying:

after dying

ive geniuenly tried every method i could come up with to fix this, its been plaguing my game for a few months now because I really want this ability to work

this is code i tried (this is server-side, client-sided is different)

local b=pl.Character:GetPivot()
local d=RaycastParams.new()
d.FilterType=Enum.RaycastFilterType.Exclude
d.FilterDescendantsInstances={pl.Character:GetChildren()}
local c=workspace:Raycast(pl.Character:GetPivot().Position,Vector3.new(100,100,100)*500,d)
if pl.Character and c.Instance.Parent:FindFirstChild("HumanoidRootPart") then pl.Character:PivotTo(c.Instance.Parent:GetPivot()) c.Instance.Parent:PivotTo(b) end

work (bit differently than yours, litreally requires right direction to process) and doesn’t teleport you to previous instance’s place

changed it to your code