ヒット商品ブログ

グルメからゲームまで

【Robloxゲームの作り方】スクリプトのサンプル~アバターを操る

LUAのサンプルのメモです。

今回はアバターの属性や動作について、プログラムから操作する場合のサンプルをまとめています。

アバター関連のサンプルスクリプト

f:id:apicode:20210519084843p:plain

 

ゲーム開始時の初期化

「ServerScriptService」フォルダに追加したスクリプトにおいて、ユーザを調べたり属性を変更します。

 

参加者の名前を出力する

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)

print(player.Name .. "が参加しました")

end)

 

Players.PlayerRemoving:Connect(function(player)

print(player.Name .. "が離脱しました")

end)

 

 

移動のスクリプト

触るとテレポート

移動元のブロック(TP1)と、移動先のブロック(TP2)を用意しておきます。

TP1にタッチしたら、アバターのCFrame(座標)をTP2へ移動します。

local function doTeleport(part) 

    local humanoid = part.Parent:FindFirstChild("HumanoidRootPart")

    if humanoid then 

       humanoid.CFrame = script.parent.TP2.CFrame + Vector3.new(0, 5, 0)

    end

end
script.parent.TP1.Touched:Connect(doTeleport)

 

触るとコンベイヤー

上記移動スクリプトの応用例で、コンベイヤーベルト上で移動するような場合です。CFrameを少しづつ動かします。

script.Parent.Touched:connect(function(hit)

    local humanoid = hit.Parent:FindFirstChild("HumanoidRootPart")

   if humanoid then

      humanoid.CFrame = humanoid.CFrame + Vector3.new(0.3, 0, 0)

 end

end)

 

触ると歩くスピードがアップ

アバターの「WalkSpeed」を設定することで、早く走ったりゆっくり走ったりさせることができます。

script.Parent.Touched:connect(function(hit)

  if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then     

          hit.Parent.Humanoid.WalkSpeed = 50

  end

end)

 

触るとジャンプ

触ったら「Jump 」をtrueにします。

script.Parent.Touched:connect(function(hit)

  if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then     

            hit.Parent.Humanoid.JumpPower = 100

            hit.Parent.Humanoid.Jump = true

  end

end)

 

触ると死ぬ

タッチしたアバターのHealthパラメータを0にします。

script.Parent.Touched:connect(function(hit)

  if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then     hit.Parent.Humanoid.Health = 0

  end

end)

 

属性を変えるスクリプト

顔のテクスチャを変更

あらかじめ顔のアセットIDが必要。「http://www.roblox.com/asset/?id=7080078」へ変更するには以下のように書きます。

実際にはDecalを活用して、IDの個所だけ外においていたほうが変更はしやすくなります。

local head = script.Parent

script.Parent.Touched:connect(function(hit)

 

    local h = hit.Parent:findFirstChild("Humanoid")

 

    if h~=nil then

        if hit.Parent:findFirstChild("Head"):findFirstChild("face").Texture == nil then return end        hit.Parent:findFirstChild("Head"):findFirstChild("face").Texture="http://www.roblox.com/asset/?id=7080078"

    end

end

 

 

Robloxでのゲームの作り方、Roblox Studioの使い方はこちら。

apicodes.hatenablog.com

このブログは、ネットや書籍上の情報、個人の体験や感想を中心にまとめたものです。 正確性を期していはいますが、間違い・誤訳等あるかもしれません。 当サイトの情報によって生じたいかなる損失について一切の責任を負わないものとします. あらかじめご了承ください。

プライバシーポリシー |〇利用規約 |〇問い合わせ