How To Make A Ray cast Gun

Hello! This is my first ever post on the dev forum so correct me if I make any mistakes

1. Make the tool
Insert A Handle
Then insert a part and put it at the front of the gun and name it Shoot
Weld the shoot part to the handle and set the transparency to 1 and set can collide to false

GUNNNN

2. Setting Up The Script And Events
Insert a local script into the gun and name it FrontEnd
Insert a Script into the gun and name in BackEnd
Insert a remote event into the gun and name it OnShoot
Insert a remote event into ReplicatedStorage and name it RayCastEvent

Rep

3. Getting The Scripts Ready
Insert A Part Into Server Storage and name it Bullet Customize it to your liking this will be the thing that comes out of the gun

Insert a part into Server Storage and name it Hole customize it to your liking this will be the hole the the bullet makes

Insert a particle into Server Storage and name it BulletEffect this will be the flash the muzzle makes when you shoot the gun

Screenshot 2020-10-15 204341

4. Making the scripts work
Now that were done setting up the gun we can make the scripts

FrontEnd :

local remote = tool:WaitForChild("OnShoot") -- the shoot event

local maxAmmo = 16 -- the max ammo of the gun
local ammo = maxAmmo -- sets the ammo to max ammo
local reloading = false -- reloading is false
local UIS = game:GetService("UserInputService") -- geting user input service

local Players = game:GetService("Players") -- geting the player service
local locplr = game:GetService("Players").LocalPlayer -- geting a local player
local client = Players.LocalPlayer -- gets the client for the local player
local cursor = client:GetMouse()  -- gets the mouse

local Camera = game.Workspace.CurrentCamera -- gets the camera
local anim = Instance.new("Animation") -- creates the anim
local strplr = game:GetService("StarterPlayer") -- starter player
anim.AnimationId = "rbxassetid://5822715091" -- idle anim id
local track -- idle anim

local reloadanim = Instance.new("Animation") -- creating the anim
reloadanim.AnimationId = "rbxassetid://5832112236" -- reload anim id
local trackreload -- reload anim

tool.Activated:Connect(function() -- when tool is fired
	if ammo > 0 and not reloading then -- checking if ammo is more than 0
		ammo = ammo - 1 -- decreasing the ammo
		Camera.CFrame = Camera.CFrame * CFrame.Angles(0,math.rad(3),0) -- recoil feel free to change
		remote:FireServer(cursor.Hit.Position) -- gets cursor pos and hit pos and fires server event
	else -- if not ammmo more than 0
		local sound = Instance.new("Sound", tool) -- creates the sound
		sound.SoundId = "rbxassetid://132464034" -- sound id
		sound.EmitterSize = 30 -- decrease the emitter size (for earlier volume drop off)
		sound:Play() -- plays no ammo sound
	end
end)

tool.Equipped:Connect(function() -- if the tool is equipped
	local function reload() -- reload
		-- Sounds/Anims
		local sound = Instance.new("Sound", tool) -- making the new sound
		sound.SoundId = "rbxassetid://4808446768" -- sound id
		sound.EmitterSize = 30 -- decrease the emitter size (for earlier volume drop off)
		sound:Play() -- plays reload sound
		trackreload = script.Parent.Parent.Humanoid:LoadAnimation(reloadanim) -- geting the anim onto the player
		trackreload.Priority = Enum.AnimationPriority.Movement -- anim Priority
		trackreload.Looped = false -- looped false
		track:Stop() -- stops idle anim
		trackreload:Play() -- plays reload anim
		--
		-- Reload
		reloading = true -- sets reloading true
		wait(3) -- waits 3 secs
		track:Play() -- plays idle anim
		ammo = maxAmmo -- makes ammo max
		reloading = false -- sets reloading false
		--
	end
	game:GetService('Players').LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson -- switching the camera
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim) -- geting the track onto the player
	track.Priority = Enum.AnimationPriority.Movement -- anim prioity
	track.Looped = true -- if looped
	track:Play() -- plays anim
	UIS.InputBegan:Connect(function(Key) -- Detecting a input
		if Key.KeyCode == Enum.KeyCode.R and reloading == false and ammo ~= maxAmmo then -- keycode r then check then reload
			reload() -- starts the reload function
		end
	end)
end)

tool.Unequipped:Connect(function() -- check if the player has the tool equiped
	game:GetService('Players').LocalPlayer.CameraMode = Enum.CameraMode.Classic -- sets camera back to normal
	if track then -- checks for a track
		track:Stop() -- stops the track
	end
end

:

BackEnd :

local shoot_part = tool:WaitForChild("Shoot") -- gets the shoot part the part were the ray come from
local remote = tool:WaitForChild("OnShoot") -- gets the remote event

local Workspace = game:GetService("Workspace") -- gets the workspace
local ServerStorage = game:GetService("ServerStorage") -- gets server storage


remote.OnServerEvent:Connect(function(player, position) -- fires when the player activates the tool
	local sound = Instance.new("Sound", tool)  -- creates the shoot sound
	sound.SoundId = "rbxassetid://1590181673"  -- shoot sound id
	sound.EmitterSize = 30 -- decrease the emitter size (for earlier volume drop off)
	sound:Play() -- plays the sound
	local origin = shoot_part.Position -- get the ray orgin
	local direction = (position - origin).Unit*300 -- when ray stops
	local result = Workspace:Raycast(origin, direction) -- the thing the ray hit

	local intersection = result and result.Position or origin + direction -- i forgot what this was lol
	local distance = (origin - intersection).Magnitude -- the distance
	
	local particle_clone = ServerStorage.BulletEffect:Clone() -- creates the particle
	particle_clone.Parent = shoot_part -- sets the parent to the shoot part

	local bullet_clone = ServerStorage.Bullet:Clone() -- creates the ray
	bullet_clone.Size = Vector3.new(0.01, 0.01, distance) -- the ray size and dis
	bullet_clone.CFrame = CFrame.new(origin, intersection)*CFrame.new(0, 0, -distance/2) -- the bullets CFrame
	bullet_clone.Parent = Workspace -- sets the parent
	
	local hole_clone = ServerStorage.Hole:Clone() -- makes the hole
	hole_clone.Size = Vector3.new(0.3, 0.3, 0.3) -- sets the size off the hole
	hole_clone.CFrame = CFrame.new (intersection)*CFrame.new(0, 0, -intersection/2) -- the holes CFrame
	hole_clone.Parent = Workspace -- sets the parent
	

	if result then -- checks the result of the ray
		local part = result.Instance -- makes a part idk again i forgot
		local humanoid = part.Parent:FindFirstChild("Humanoid") or part.Parent.Parent:FindFirstChild("Humanoid") -- checks for a humanoid
		local head = part.Parent:FindFirstChild("Humanoid") and part.Parent.Parent:FindFirstChild("Head") -- checks if it is a headshot
		
		if head then -- if it is a headshot
			hole_clone:Destroy() -- destroys the bullet hole
			humanoid:TakeDamage(70) -- takes damage
		end

		if humanoid then -- if body shot
			hole_clone:Destroy() -- destroys the hole
			humanoid:TakeDamage(40) -- takes damage
		end
	end

	wait(0.25) -- waits 0.25
	particle_clone:Destroy() -- destroys the particle_clone
	bullet_clone:Destroy() -- destroys the ray
	wait(7) -- waits 7
	hole_clone:Destroy() -- destroys the bullet hole
end)

:

Thats It!

Again I’m new to the dev forum so if I did something wrong then tell me!

Credit : I used a modified script the original script is here How to make a raycasting gun All credit goes to him

So you’re essentially reuploading another person’s community tutorial? There’s really no point in creating another tutorial with very similar code that was originally their tutorial.

2 Likes

Well i added alot of things and gave credit to him but if I have to to ill delete the post im sorry