#Camera & Position Issue

1 messages · Page 1 of 1 (latest)

dry ibex
#

I've analyzed your code and created a complete solution that fixes both the position reset and camera direction issues. The key is to preserve the character's position and camera state during the swap.

#

@forest prairie the script is a lil long dm ma

#

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local CharSwapEv = RS.Events.CharacterSwap

local ScreenGui = script.Parent.Parent
local B1 = ScreenGui.Buttons.FirstCharacter
local B2 = ScreenGui.Buttons.SecondCharacter

-- Store position and camera state before swapping
local function Pressed(Button)
local charName
if Button == B1 then
charName = "Dummy1"
elseif Button == B2 then
charName = "Dummy2"
end

if player.Character and player.Character.Name == charName then
    return
end
#

-- Store current position and camera state
local currentCharacter = player.Character
local position, cameraCFrame

if currentCharacter then
    local humanoidRootPart = currentCharacter:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        position = humanoidRootPart.CFrame
    end
end

-- Store camera state
local camera = workspace.CurrentCamera
cameraCFrame = camera.CFrame

-- Send both character name and position to server
CharSwapEv:FireServer(charName, position)

-- Wait for new character
local newChar = player.CharacterAdded:Wait()
local newHum = newChar:WaitForChild("Humanoid")
local newHRP = newChar:WaitForChild("HumanoidRootPart")
#

-- Restore camera position after character loads
newHRP:WaitForChild("BodyPosition") -- Wait for server to set position
task.wait(0.1) -- Small delay to ensure position is set
camera.CFrame = cameraCFrame
end

-- Keyboard input handling
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.One then
Pressed(B1)
elseif input.KeyCode == Enum.KeyCode.Two then
Pressed(B2)
end
end)

-- Button click handling
B1.MouseButton1Click:Connect(function()
Pressed(B1)
end)

B2.MouseButton1Click:Connect(function()
Pressed(B2)
end)

#

local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local CharSwapEv = RS.Events.CharacterSwap

CharSwapEv.OnServerEvent:Connect(function(plr, SelectedChar, position)
local canSwap = plr:WaitForChild("PlayerValues"):WaitForChild("canSwap")
local CurrentChar = plr:WaitForChild("PlayerValues"):WaitForChild("CurrentCharacter")

if not canSwap.Value then
    return
end

if CurrentChar.Value == SelectedChar then
    return
end

local Morphs = SS.Characters
local Morph = Morphs:FindFirstChild(tostring(SelectedChar))
...

end)

forest prairie
#

it still doesnt work

--- LOCAL ---

local RS = game:GetService("ReplicatedStorage")
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer

local CharSwapEv = RS.Events.CharacterSwap

local ScreenGui = script.Parent.Parent
local B1 = ScreenGui.Buttons.FirstCharacter
local B2 = ScreenGui.Buttons.SecondCharacter

-- Store position and camera state before swapping
local function Pressed(Button)
local charName
if Button == B1 then
charName = "Dummy1"
elseif Button == B2 then
charName = "Dummy2"
end

if player.Character and player.Character.Name == charName then
    return
end
-- Store current position and camera state
local currentCharacter = player.Character
local position, cameraCFrame

if currentCharacter then
    local humanoidRootPart = currentCharacter:FindFirstChild("HumanoidRootPart")
    if humanoidRootPart then
        position = humanoidRootPart.CFrame
    end
end

-- Store camera state
local camera = workspace.CurrentCamera
cameraCFrame = camera.CFrame

-- Send both character name and position to server
CharSwapEv:FireServer(charName, position)

-- Wait for new character
local newChar = player.CharacterAdded:Wait()
local newHum = newChar:WaitForChild("Humanoid")
local newHRP = newChar:WaitForChild("HumanoidRootPart")
-- Restore camera position after character loads
newHRP:WaitForChild("BodyPosition") -- Wait for server to set position
task.wait(0.1) -- Small delay to ensure position is set
camera.CFrame = cameraCFrame

end

-- Keyboard input handling
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.KeyCode == Enum.KeyCode.One then
Pressed(B1)
elseif input.KeyCode == Enum.KeyCode.Two then
Pressed(B2)
end
end)

-- Button click handling
B1.MouseButton1Click:Connect(function()
Pressed(B1)
end)

B2.MouseButton1Click:Connect(function()
Pressed(B2)
end)

--- SERVER ---
local RS = game:GetService("ReplicatedStorage")
local SS = game:GetService("ServerStorage")
local Players = game:GetService("Players")

local CharSwapEv = RS.Events.CharacterSwap

CharSwapEv.OnServerEvent:Connect(function(plr, SelectedChar, position)
local canSwap = plr:WaitForChild("PlayerValues"):WaitForChild("canSwap")
local CurrentChar = plr:WaitForChild("PlayerValues"):WaitForChild("CurrentCharacter")

if not canSwap.Value then
    return
end

if CurrentChar.Value == SelectedChar then
    return
end

local Morphs = SS.Characters
local Morph = Morphs:FindFirstChild(tostring(SelectedChar))

if Morph then
    local MorphClone = Morph:Clone()
    MorphClone.Name = plr.Name
    MorphClone.Parent = workspace
    plr.Character = MorphClone
    CurrentChar.Value = SelectedChar

    canSwap.Value = false
    
    local ScreenGui = plr.PlayerGui:WaitForChild("ScreenGui")
    local B1 = ScreenGui.Buttons.FirstCharacter
    local B2 = ScreenGui.Buttons.SecondCharacter
    B1.BackgroundTransparency = 0.8
    B2.BackgroundTransparency = 0.8

    task.delay(5, function()
        canSwap.Value = true
        B1.BackgroundTransparency = 0
        B2.BackgroundTransparency = 0
    end)
end

end)

i tried this way too but for some reason it doesnt work

#

i asked gpt too but it broke everything