Character viewportframe duplication

You can write your topic however you want, but you need to answer these questions:
Hey guys im making a inventory system for a survival game and its got a character viewport frame to show the character when you open your inventory. however when you open your inventory it duplicates the character each time in the viewport and also your footsteps get duplicated aswell.

image

local tweenService = game:GetService("TweenService")
local plrs = game:GetService("Players")
local usi = game:GetService("UserInputService")
local player = plrs.LocalPlayer
local char = player.Character
local gui = player.PlayerGui.InventoryGUI.InventoryFrame:WaitForChild("MainBG")
local viewportFrame = gui.Frame:WaitForChild("ViewportFrame") -- Your ViewportFrame inside the inventory
local Opened = false
local Debounce = false -- Prevent spam inputs

-- Define the tween properties
local tweenInfo = TweenInfo.new(0.1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local goalPosition = UDim2.new(0.15, 0, 0.048, 0) -- Center position
local hiddenPosition = UDim2.new(0.15, 0, -0.800, 0) -- Off-screen above



local function setupViewport()
    local MouseInDisplay, HoldInDisplay = false, false

    local currentX
    
    -- Character Display
    local VPFcam = Instance.new("Camera"); VPFcam.Parent = viewportFrame
    VPFcam.CFrame = CFrame.new(0,0,0)
    viewportFrame.CurrentCamera = VPFcam

    repeat wait(.1) until game:IsLoaded()

    char.Archivable = true

    local ClonedChar = char:Clone()

    ClonedChar.Parent = viewportFrame.WorldModel
    ClonedChar.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
    ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-6.5),Vector3.new(0,0,0)))
end


local function toggleGUI()
    if Debounce then return end -- Block input if an animation is running
    Debounce = true

    if Opened then
        Opened = false
        local tweenOut = tweenService:Create(gui, tweenInfo, {Position = hiddenPosition})
        tweenOut:Play()
        tweenOut.Completed:Connect(function()
            gui.Visible = false
            Debounce = false -- Allow new inputs after the animation completes
        end)
    else
        Opened = true
        gui.Visible = true
        gui.Position = hiddenPosition -- Start position (off-screen above)

		-- Create and play the tween
		local tweenIn = tweenService:Create(gui, tweenInfo, {Position = goalPosition})
		tweenIn:Play()
		tweenIn.Completed:Connect(function()
			Debounce = false -- Allow new inputs after the animation completes
		end)

		-- Setup viewport when opening inventory
		setupViewport()
	end
end

local function inputBegun(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.Tab then
		toggleGUI()
	end
end

just use an if statement to check for an existing character model then return the function to prevent duplication

Try like this and also i recommend to move the camera cframe inside the viewportframe to the character model position instead of setting the character model cframe:

local function setupViewport()
    local MouseInDisplay, HoldInDisplay = false, false

    local currentX
     
    -- Character Display
    if viewportFrame.WorldModel:FindFirstChildOfClass("Model") then return end --prevent duplicates
    local VPFcam = Instance.new("Camera"); VPFcam.Parent = viewportFrame
    VPFcam.CFrame = CFrame.new(0,0,0)
    viewportFrame.CurrentCamera = VPFcam

    repeat wait(.1) until game:IsLoaded()

    char.Archivable = true

    local ClonedChar = char:Clone()

    ClonedChar.Parent = viewportFrame.WorldModel
    ClonedChar.Humanoid.DisplayDistanceType = Enum.HumanoidDisplayDistanceType.None
    ClonedChar:SetPrimaryPartCFrame(CFrame.new(Vector3.new(0,0,-6.5),Vector3.new(0,0,0)))
end
1 Like

legend cheers for the help, such a simple fix ahaha

your welcome glad i could help

well the foot steps still duplicate still but the character model is fine

by footsteps do you mean in the viewportframe