プレイヤーの上に文字を表示

BillboardGui を使い、キャラクターの頭上にカメラの方向を向く文字を表示します。

設定とコード例

ServerScriptService に Script を追加して使います。この例では、Playerの Wins 属性が50以上なら「Commander」、100以上なら「Captain」と表示します。属性がまだなければ、勝利数は0として扱います。

local Players = game:GetService("Players")

local function getRankForPlayer(player)
    local wins = player:GetAttribute("Wins") or 0
    if wins >= 100 then
        return "Captain"
    elseif wins >= 50 then
        return "Commander"
    end
    return "Seaman"
end

-- プレイヤーがキャラクターを持ったときに階級を表示する
Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        -- BillboardGuiを作成
        local billboardGui = Instance.new("BillboardGui")
        billboardGui.Size = UDim2.new(4, 0, 1, 0) -- サイズ調整
        billboardGui.StudsOffset = Vector3.new(0, 3, 0) -- キャラクターの頭の上に表示
        billboardGui.AlwaysOnTop = true

        -- TextLabelを作成して階級を表示
        local textLabel = Instance.new("TextLabel", billboardGui)
        textLabel.Size = UDim2.new(1, 0, 1, 0)
        textLabel.BackgroundTransparency = 1
        textLabel.TextColor3 = Color3.new(1, 1, 1) -- 白色
        textLabel.TextStrokeTransparency = 0.5 -- 縁取りを追加して読みやすくする
        textLabel.TextScaled = true -- テキストサイズを自動調整
        textLabel.Font = Enum.Font.SourceSansBold

        -- 階級を取得してTextLabelに表示
        local rank = getRankForPlayer(player) -- プレイヤーの階級を取得する関数を用意
        textLabel.Text = rank

        -- HeadパーツにBillboardGuiをアタッチ
        local head = character:WaitForChild("Head")
        billboardGui.Parent = head
    end)
end)

動作を確かめる

リスポーン後も文字が正しいキャラクターの頭上に表示されるか確認します。

逆引きリファレンスの一覧に戻る