[로블록스 스튜디오] 간단한 총기 스크립트! 서버&클라이언트 코드 포함
로블록스에서 간단한 무기 스크립트를 원한다면, 기본적인 총기 스크립트를 추천 해드리겠습니다. 이 스크립트는 Tool을 사용해 플레이어가 장착하면 클릭 시 총알을 발사하는 기능을 포함하고 있어요!
1. 무기 툴 만들기
- 로블록스 Studio에서 StarterPack 안에 Tool을 추가
- Tool 안에 Part를 추가하고, Handle로 이름을 변경
- Tool 안에 LocalScript를 추가해 아래 코드를 입력
2. 로컬 스크립트 (클라이언트)
📌 Tool 안에 LocalScript 추가
local tool = script.Parent
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
tool.Activated:Connect(function()
if tool.Enabled then
tool.Enabled = false -- 연속 발사 방지
-- 서버에 총알 발사 요청
game.ReplicatedStorage.FireWeapon:FireServer(mouse.Hit.p)
wait(0.3) -- 쿨타임
tool.Enabled = true
end
end)
3. 서버 스크립트 (총알 생성)
📌 ServerScriptService 안에 Script 추가하고 아래 코드 입력
local replicatedStorage = game:GetService("ReplicatedStorage")
-- 원격 이벤트 생성
local fireEvent = Instance.new("RemoteEvent", replicatedStorage)
fireEvent.Name = "FireWeapon"
fireEvent.OnServerEvent:Connect(function(player, targetPosition)
local bullet = Instance.new("Part")
bullet.Size = Vector3.new(0.3, 0.3, 1.5)
bullet.Color = Color3.new(1, 0, 0) -- 빨간색 총알
bullet.Material = Enuhttp://m.Material.Neon
bullet.Anchored = false
bullet.CanCollide = false
bullet.Position = player.Character.Head.Position
bullet.Parent = game.Workspace
-- 총알 이동 방향 설정
local direction = (targetPosition - bullet.Position).unit
local velocity = direction * 100 -- 속도
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = velocity
bodyVelocity.Parent = bullet
-- 3초 후 총알 제거
game:GetService("Debris"):AddItem(bullet, 3)
end)
4. 실행 방법
- StarterPack에 Tool을 넣고, 실행 후 툴을 장착한 뒤 클릭하면 총알이 발사됩니다.
- FireWeapon 이벤트를 활용해 더 다양한 무기를 만들 수도 있어요!