﻿-- 動く足場のスクリプト
-- サービスの取得
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

-- 足場のパーツを作成
local part = Instance.new("Part")
part.Size = Vector3.new(6, 1, 6)
part.Position = Vector3.new(0, 5, 0)
part.Anchored = true
part.BrickColor = BrickColor.new("Bright green")
part.Shape = Enum.PartType.Block
part.Parent = workspace

-- アニメーションの情報
local tweenInfo = TweenInfo.new(
  5,                           -- 実行する時間
  Enum.EasingStyle.Linear,     -- 加減速のスタイル
  Enum.EasingDirection.InOut,  -- 実行する方向
  -1,                          -- 繰り返す回数
  true,                        -- 往復させる
  0                            -- 開始までの遅延時間
)

-- アニメーションのオブジェクトを作成
local tween = TweenService:Create(
  part, tweenInfo, {
    Position = Vector3.new(0, 5, -50)
  }
)

-- アニメーションを開始
tween:Play()

-- 1フレーム前の足場の座標
local oldPosition = part.Position

-- 足場の移動速度を算出
local function GetVelocity(time, deltaTime)
  local nowPosition = part.Position
  local velocity = (nowPosition - oldPosition) / deltaTime
  part.AssemblyLinearVelocity = velocity
  oldPosition = nowPosition
end

-- フレームごとに関数を呼び出し
RunService.Stepped:Connect(GetVelocity)
