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
- 1In Roblox Studio, add a Tool to StarterPack. Add a Part named Handle inside it for the gun itself, and a RemoteEvent named Fire.
- 2Hover over the Tool in the Explorer, press +, choose Script and paste in the first script.
- 3Do the same with a LocalScript for the second, then press Play with a dummy or a friend to shoot at.
-- 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)-- 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.
FilterDescendantsInstanceskeeps the shot from hitting the shooter.TakeDamagerespects a ForceField, so players who've just spawned are safe.
Make it yours
- Change how it feels
DAMAGE,RANGEandSECONDS_BETWEEN_SHOTSare at the top of the first script.- Add bullets, a reload and sound
- Ask the script generator to start from this.
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
Roblox kill brick scriptRoblox leaderboard scriptRoblox save data scriptRoblox sprint scriptRoblox double jump scriptRoblox teleport scriptRoblox obby checkpoint scriptRoblox coin pickup scriptRoblox day and night cycle scriptRoblox game pass scriptRoblox developer product scriptRoblox door scriptRoblox tool giver scriptRoblox round system scriptRoblox background music scriptRoblox speed pad scriptRoblox jump pad scriptRoblox VIP door scriptRoblox daily reward scriptRoblox NPC dialogue scriptRoblox pet follow scriptRoblox admin commands scriptRoblox tycoon dropper scriptRoblox shop GUI script

