Character does not stay in the same position when switched (Morph)

How can I keep the character in the same position, Thank you!

local players = game:GetService"Players"
local starterPlayer = game:GetService"StarterPlayer"
local replicated = game:GetService"ReplicatedStorage"
local morphRemote = replicated.MorphRemote
local morphsFolder = replicated.MorphsFolder

local function onMorphRemoteFired(player, model)
	players.CharacterAutoLoads = false
	local modelClone = model:Clone()
	modelClone.Parent = starterPlayer
	player:LoadCharacter()
	modelClone:Destroy()
	players.CharacterAutoLoads = true
	for _, _player in ipairs(players:GetPlayers()) do
		if _player ~= player then
			if not _player.Character then
				_player:LoadCharacter()
			end
		end
	end
end

morphRemote.OnServerEvent:Connect(onMorphRemoteFired)
local tweens = game:GetService"TweenService"
local replicated = game:GetService"ReplicatedStorage"
local morphRemote = replicated:WaitForChild"MorphRemote"

local morphGui = script.Parent
local toggleButton = morphGui:WaitForChild"ToggleButton"
local morphFrame = morphGui:WaitForChild"MorphFrame"
local morphsFrame = morphFrame:WaitForChild"MorphsFrame"

local toggle = true
local debounce = false

local function onToggleButtonClicked()
	if debounce then return end
	debounce = true
	toggle = not toggle
	local direction = if toggle then "In" else "Out"
	local position = if toggle then 0 else -0.3
	local tween = tweens:Create(morphFrame, TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection[direction]), {Position = UDim2.new(position, 0, 0.4, 0)})
	tween:Play()
	tween.Completed:Wait()
	toggleButton.Text = if toggle then "<" else ">"
	debounce = false
end

toggleButton.MouseButton1Click:Connect(onToggleButtonClicked)

local function onMorphButtonClicked(morphButton)
	local morphFrame = morphButton.Parent
	local objectValue = morphFrame:FindFirstChild"Value"
	if objectValue then
		if objectValue.Value then
			morphRemote:FireServer(objectValue.Value)
		end
	end
end

for _, morphFrame in ipairs(morphsFrame:GetChildren()) do
	if morphFrame:IsA"Frame" then
		local morphButton = morphFrame:WaitForChild"MorphButton"
		morphButton.MouseButton1Click:Connect(function()
			onMorphButtonClicked(morphButton)
		end)
	end
end

I guess you could get the position of the character before you clone the morph, then teleport the player after _player:LoadCharacter().

I’m making my first game, I don’t know how to solve this problem, could you do this for me? Thanks for answering!

You can do something like this:

local function onMorphRemoteFired(player, model)
	local char = player.Character or player.CharacterAdded:Wait()
	local defaultCFrame = char.PrimaryPart.CFrame
	local con
	
	players.CharacterAutoLoads = false
	local modelClone = model:Clone()
	modelClone.Parent = starterPlayer
	player:LoadCharacter()
	modelClone:Destroy()
	players.CharacterAutoLoads = true
	for _, _player in ipairs(players:GetPlayers()) do
		if _player ~= player then
			if not _player.Character then
				_player:LoadCharacter()
				
				con = _player.CharacterAdded:Connect(function()
					_player.Character:SetPrimaryPartCFrame(defaultCFrame)
					con:Disconnect()
				end)
			end
		end
	end
end

Not sure if this works, you’re going to have to test it yourself.

It didn’t work, you can have a look…
test.rbxl (88.2 KB)

You could save the Position of the Character before they morph
I can’t give you the full code but I’ll give you abit of code

local OldCharacterHumanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")
--After the Player Morphs
local NewCharacterHumanoidRootPart = Player.Character:FindFirstChild("HumanoidRootPart")

NewCharacterHumanoidRootPart.Position = OldCharacterHumanoidRootPart.Position
-- I recommend you making it CFrame. If you do it with CFrame then Change The Position to CFrame.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.