Skip to content

Instantly share code, notes, and snippets.

@gamebox777
Last active April 27, 2024 15:10
Show Gist options
  • Select an option

  • Save gamebox777/ccb84e1a86d49df2d1f4c99102a34caf to your computer and use it in GitHub Desktop.

Select an option

Save gamebox777/ccb84e1a86d49df2d1f4c99102a34caf to your computer and use it in GitHub Desktop.
roblox:ModuleScriptFireBullet
-- モジュールの初期化
local module = {}
-- RobloxのReplicatedStorageサービスを取得
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- ReplicatedStorageから「Bullet01」という名前の子要素(弾のテンプレート)を待機して取得
local bulletTemplate = ReplicatedStorage:WaitForChild("Bullet01")
-- 弾を発射する関数定義。引数playerには、弾を発射するプレイヤーの情報が渡される。
function module.fireBullet(player)
-- プレイヤーのキャラクターを取得。キャラクターがまだロードされていない場合はロードを待つ
local character = player.Character or player.CharacterAdded:Wait()
-- 弾のテンプレートから新しい弾をクローン
local bulletClone = bulletTemplate:Clone()
-- クローンした弾の位置をプレイヤーの頭の位置に設定
bulletClone.Position = character.Head.Position
-- 弾の初速度を設定。キャラクターの頭部の向き(LookVector)に速度(50)を掛ける
bulletClone.Velocity = character.Head.CFrame.LookVector * 50
-- 弾をゲームのワールド(Workspace)に追加
bulletClone.Parent = game.Workspace
-- 弾が動くようにアンカーを解除
bulletClone.Anchored = false
-- 弾の跳ね返りを制御するための反発係数を設定
bulletClone.Elasticity = 10
-- 弾を10秒後に自動的に消去するためにDebrisサービスを使用
game.Debris:AddItem(bulletClone, 10)
end
-- モジュールを返す
return module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment