﻿-- コインのスクリプト
-- サービスの取得
local Players = game:GetService("Players")

-- 疑似乱数の初期化
local random = Random.new()

-- コイン衝突のイベント
local function onCoinTouched(otherPart, coin)
  if coin:GetAttribute("Enabled") then
    local character = otherPart.Parent
    local player = Players:GetPlayerFromCharacter(character)
    -- プレイヤーがコインに触れた場合
    if player then
      -- コインを消去
      coin.Transparency = 1
      coin:SetAttribute("Enabled", false)
      print("Player collected coin")
      -- クールダウン
      task.wait(10)
      -- コインを表示
      coin.Transparency = 0
      coin:SetAttribute("Enabled", true)
    end
  end
end

-- コイン作成のループ
for i = 1, 15 do
  local x = random:NextInteger(-5, 5)
  local y = random:NextInteger(8, 16)
  local z = random:NextInteger(-50, -5)
  local angle = random:NextInteger(0, 90)

  -- コインのパーツを作成
  local part = Instance.new("Part")
  part.Size = Vector3.new(0.5, 3, 3)
  part.Position = Vector3.new(x, y, z)
  part.Orientation = Vector3.new(0, angle, 0)
  part.Anchored = true
  part.Material = Enum.Material.Metal
  part.BrickColor = BrickColor.new("Gold")
  part.Shape = Enum.PartType.Cylinder
  part.CanCollide = false

  -- コイン衝突時のイベントを設定
  part:SetAttribute("Enabled", true)
  part.Touched:Connect(
    function(otherPart)
      onCoinTouched(otherPart, part)
    end
  )
  part.Parent = workspace	
end

