Quick actions

cmd+k|ctrl+k

Navigation

Languages

MoonFloat

Snippet info

Language

Lua

Visibility

public

Author

caveirinhagames

Created

2024-12-23T20:05:50.436787Z

Updated

2024-12-23T20:05:50.436787Z

-- Place this script in a Roblox Script Builder like Lua Sandbox
-- It will move itself to StarterPlayerScripts and function as expected

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local StarterPlayer = game:GetService("StarterPlayer")

-- Configuration
local ORBIT_RADIUS = 3 -- Distance from player's head
local ORBIT_SPEED = 2 -- Speed of rotation
local HOVER_HEIGHT = 1.5 -- Height above player's head
local MESH_ID = "rbxassetid://14055001063" -- Example mesh ID (spacecraft)
local TEXTURE_ID = nil -- Set a texture ID or leave as nil if unused

-- Main Script
local function createFloatingPart(character)
    -- Create the part
    local floatingPart = Instance.new("Part")
    floatingPart.Name = "FloatingMeshPart"
    floatingPart.Color = Color3.fromRGB(0, 16, 176)
    floatingPart.Material = Enum.Material.Neon
    floatingPart.Size = Vector3.new(1, 1, 1)
    floatingPart.CanCollide = false
    floatingPart.Anchored = true

    -- Create and configure the mesh
    local mesh = Instance.new("FileMesh")
    mesh.MeshId = MESH_ID
    if TEXTURE_ID then
        mesh.TextureId = TEXTURE_ID
    end
    mesh.Scale = Vector3.new(0.5, 0.5, 0.5)
    mesh.Parent = floatingPart

    -- Parent the part to workspace
    floatingPart.Parent = workspace

    -- Animation variables
    local angle = 0

    -- Connect the animation update
    local connection
    connection = RunService.Heartbeat:Connect(function(deltaTime)
        if not character.Parent then
            floatingPart:Destroy()
            connection:Disconnect()
            return
        end

        local head = character:FindFirstChild("Head")
        if head then
            -- Calculate position
            angle = angle + deltaTime * ORBIT_SPEED
            local newPosition = head.Position + Vector3.new(
                math.cos(angle) * ORBIT_RADIUS,
                HOVER_HEIGHT,
                math.sin(angle) * ORBIT_RADIUS
            )

            -- Update position
            floatingPart.Position = newPosition
            floatingPart.CFrame = CFrame.new(newPosition, head.Position)
        end
    end)

    return floatingPart
end

-- Script Placement in StarterPlayerScripts
if StarterPlayer:FindFirstChild("StarterPlayerScripts") then
    local scriptClone = script:Clone()
    scriptClone.Parent = StarterPlayer.StarterPlayerScripts
    script:Destroy() -- Remove this script from the current location
end

-- Setup for the local player
local player = owner
local function onCharacterAdded(character)
    createFloatingPart(character)
end

if player.Character then
    onCharacterAdded(player.Character)
end
player.CharacterAdded:Connect(onCharacterAdded)
INFO