Sure! Below is a simple Roblox gun script that you can use in your game. This script includes basic functionality such as:
- Firing projectiles (bullets)
- Ammo system
- Cooldown between shots
- Basic sound and visual effects
This script is designed to be placed inside a tool in Roblox Studio.
🔧 Step-by-step Instructions:
- Open Roblox Studio
- Insert a Tool into your game (Insert > Template > Tools > Empty Tool or create one manually)
- Rename the tool something like
Pistol or Gun
- Inside the tool, insert a Script (not a LocalScript)
- Paste the following script into it
📜 Gun Script (Server-Side)
-- Gun Tool Script
local tool = script.Parent
local ammo = 10
local maxAmmo = 10
local fireRate = 0.3 -- seconds between shots
local bulletSpeed = 100
local reloadTime = 1.5
local isReloading = false
local canFire = true
-- Create a handle if none exists
if not tool:FindFirstChild("Handle") then
local handle = Instance.new("Part")
handle.Name = "Handle"
handle.Size = Vector3.new(1, 1, 3)
handle.Anchored = false
handle.CanCollide = false
handle.Parent = tool
local mesh = Instance.new("BlockMesh")
mesh.Scale = Vector3.new(0.3, 0.3, 0.6)
mesh.Parent = handle
end
tool.Equipped:Connect(function()
print("Gun equipped")
end)
tool.Unequipped:Connect(function()
print("Gun unequipped")
end)
tool.Activated:Connect(function()
if not canFire or isReloading or ammo <= 0 then return end
-- Fire bullet
canFire = false
ammo -= 1
print("Fired! Ammo left: " .. ammo)
local player = game.Players:GetPlayerFromCharacter(tool.Parent.Parent)
local character = player.Character
if not character then return end
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
if not humanoidRootPart then return end
-- Create bullet
local bullet = Instance.new("Part")
bullet.Shape = Enum.PartType.Ball
bullet.Size = Vector3.new(0.5, 0.5, 0.5)
bullet.BrickColor = BrickColor.Red()
bullet.Material = Enum.Material.SmoothPlastic
bullet.Anchored = false
bullet.CanCollide = true
bullet.CFrame = tool.Handle.CFrame
bullet.Parent = workspace
-- Add velocity
local direction = (humanoidRootPart.CFrame.LookVector) * bulletSpeed
bullet.Velocity = direction
-- Clean up bullet after 3 seconds
spawn(function()
wait(3)
if bullet and bullet.Parent then
bullet:Destroy()
end
end)
-- Apply cooldown
wait(fireRate)
canFire = true
end)
-- Reload function
tool.RequiresHandle = true
tool:GetPropertyChangedSignal("Parent"):Connect(function()
if tool.Parent and tool.Parent:IsA("Backpack") then
isReloading = false
elseif tool.Parent and tool.Parent:IsA("Humanoid") then
tool.Parent:Equip tool()
end
end)
Optional Enhancements:
You can add more features like:
- Reload mechanic using
UserInputService (Client-side) + RemoteEvent
- Damage using
Touched event on bullet
- Gunshot Sound
- Muzzle Flash
Would you like help with any of those next?