Forja

Roblox gun script

A gun for your own game: click to fire, and the server decides what it hit. Damage, range and fire rate are all on the server, so a cheater can't change them.

Where it goes

  1. 1In Roblox Studio, add a Tool to StarterPack. Add a Part named Handle inside it for the gun itself, and a RemoteEvent named Fire.
  2. 2Hover over the Tool in the Explorer, press +, choose Script and paste in the first script.
  3. 3Do the same with a LocalScript for the second, then press Play with a dummy or a friend to shoot at.
GunServer · a Script in the Tool
-- From forja.us/tools/roblox-scripts/gun-- Fires on the server, so players can't cheat the damage, range or fire rate.local DAMAGE = 25local RANGE = 300local SECONDS_BETWEEN_SHOTS = 0.4local tool = script.Parentlocal fire = tool:WaitForChild("Fire")local lastShot = 0fire.OnServerEvent:Connect(function(player, target)	local character = tool.Parent	if character ~= player.Character or typeof(target) ~= "Vector3" then		return	end	local now = os.clock()	if now - lastShot < SECONDS_BETWEEN_SHOTS then		return	end	local head = character:FindFirstChild("Head")	local humanoid = character:FindFirstChildOfClass("Humanoid")	if not head or not humanoid or humanoid.Health <= 0 then		return	end	local aim = target - head.Position	if not (aim.Magnitude > 0.1) then		return	end	lastShot = now	local params = RaycastParams.new()	params.FilterType = Enum.RaycastFilterType.Exclude	params.FilterDescendantsInstances = { character }	local result = workspace:Raycast(head.Position, aim.Unit * RANGE, params)	local victim = result and result.Instance:FindFirstAncestorOfClass("Model")	local theirHumanoid = victim and victim:FindFirstChildOfClass("Humanoid")	if theirHumanoid then		theirHumanoid:TakeDamage(DAMAGE)	endend)
GunClient · a LocalScript in the Tool
-- From forja.us/tools/roblox-scripts/gun-- Tells the server where the player clicked.local Players = game:GetService("Players")local tool = script.Parentlocal fire = tool:WaitForChild("Fire")local mouse = Players.LocalPlayer:GetMouse()tool.Activated:Connect(function()	fire:FireServer(mouse.Hit.Position)end)

How it works

  • The LocalScript only says where the player clicked. The Script checks the fire rate and does the raycast, so a cheater can't send more damage or shoot faster.
  • FilterDescendantsInstances keeps the shot from hitting the shooter.
  • TakeDamage respects a ForceField, so players who've just spawned are safe.

Make it yours

Change how it feels
DAMAGE, RANGE and SECONDS_BETWEEN_SHOTS are at the top of the first script.
Add bullets, a reload and sound
Ask the script generator to start from this.
Want something more? The script generator can write it: “A gun tool with 12 bullets, a reload on R and a sound, with damage checked on the server”

Maps to try it on: arena and combat maps

Start from a finished map, and your scripts have a game to run in.

More free Roblox scripts