Plane disappearing when i move with my plane that i spawn

Hello fellow developers, I am working on a plane spawning menu for a game with plane crash physics and other plane-related elements. However, I have encountered an issue. I have implemented a button to spawn the plane, but when I spawn the plane while stationary, it appears next to the player. Yet, if I walk a little and stand still before spawning it again, the plane disappears, leaving only an “aimpart” part in the script, within the workspace, along with some scripts. Subsequently, attempting to spawn the plane again proves unsuccessful. Even after resetting, if I refrain from walking before spawning, it works. Yet, if I walk and then attempt to spawn, it fails. Can anyone assist me in resolving this issue?

ill show a video tomorrow if i need to show one because i might have to go today ill read everything that you guys are saying and i will try everything. please also help me fix an issue that when i test the game the plane already spawned.


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

local spawnPlaneEvent = ReplicatedStorage:WaitForChild("SpawnPlaneEvent")

-- Keep track of spawned planes
local spawnedPlanes = {}

local function cloneModel(original)
    local clone = original:Clone()

    -- Ensure all parts of the model are descendants
    for _, part in pairs(clone:GetDescendants()) do
        if part:IsA("BasePart") then
            part.Transparency = 0 -- Ensure parts are visible
        end
    end

    return clone
end

local function spawnPlane(player)
    -- Remove the oldest spawned plane, if any
    local oldestPlane = table.remove(spawnedPlanes, 1)
    if oldestPlane then
        oldestPlane:Destroy()
    end

    local planeModel = cloneModel(ServerStorage:WaitForChild('Plane'))
    planeModel.Parent = workspace

    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild('Humanoid')

    -- Set the CFrame of the entire model based on the humanoid's position
    local offset = Vector3.new(-1, -5, 0) -- You can adjust this offset as needed
    local newPosition = humanoid.Parent.PrimaryPart.CFrame:PointToWorldSpace(offset)

    -- Set the CFrame of all parts in the model
    for _, part in pairs(planeModel:GetDescendants()) do
        if part:IsA("BasePart") then
            part.CFrame = CFrame.new(newPosition) * part.CFrame
        end
    end

    -- Add the new plane to the list of spawned planes
    table.insert(spawnedPlanes, planeModel)
end

spawnPlaneEvent.OnServerEvent:Connect(spawnPlane)

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function()
        spawnPlane(player)
    end)
end)
1 Like