FPS Tutorial [Part 3]

Hello everyone and this is the third part to the fps tutorial. Its the major part of the series!

Disclaimer: I am new to FPS making so please point out any errors or mistakes. in this part I will show you how I did it so please don’t tell me that its the wrong way. I also recommend you read every single word as this is complex and hard.

In this part we will make the gun shoot with tracers, bullet drop and travel time!

Lets begin!

1. Resources

  • For this tutorial you will need FastCast: Redux
  • You will need basic knowledge of scripting

2. Setting up
Remember the folder called resources? We will be storing our fast cast and bullets in there.
First of all get the fast cast module and drop it in your game. Now place it in the resources.
image
Next we will make a remote event and place it in resources again. Call it something like fire or shoot. (You can read more about remote events here.)
image
Thats all we are gonna need for now.

  • Now get your gun model and inside the handle add an attachment, call it muzzle and position it at the front of the barrel.
    image
    image
  • Now drop the gun back to the models folder.
  • Next we will make a script inside server script service and call it something like server handler or something.
    image
  • And inside workspace make a folder and call it ignore.
    image

3. Scripting
This is the big part so I will divide it into three segments.
3.1 Client
Open up the client framework and lets get to work.

  • Lets start by defining our variables
local resources = rs:WaitForChild("resources")
  • We will create a function for shooting so we dont have a huge spam inside the mouse down.
function fire()
	--pew pew
end
  • Next we will need to detect when the player wants to shoot to call the function.
mouse.Button1Down:Connect(function()
	fire() --calling the function
end)
  • Lets get back to the function
    I will try my best to explain this part.
function fire()
	local origin = currentweapon.Handle.muzzle.WorldPosition --the muzzle position
	local direction = (mouse.Hit.Position - origin).Unit --the direction like: to mouse from muzzle
	--------------------------------------------------------
	resources.fire:FireServer(origin, direction, 500) --firing the event 500 is the bullet speed
end
  • Another thing we will need later is the mouse filter:
mouse.TargetFilter = cam --put this inside the first line of render stepped

3.2 Server script
Now this is the hardest part and I will also try my best to explain as best as I can.

  • Inside the server handler we will add a few variables:
local rs = game:GetService("ReplicatedStorage")
local resources = rs:WaitForChild("resources")
local debris = game:GetService("Debris")
local ignore = workspace:WaitForChild("ignore")
  • And now we will start with fast cast! Make a few variables
--fastcast
local fastcast = require(resources.FastCastRedux)
fastcast.VisualizeCasts = true --debugging leave that on for now
local caster = fastcast.new() -- the thing that will fire
  • To make it shoot we will need to catch the function first like this:
resources.fire.OnServerEvent:Connect(function()
    -- we will put stuff here soon
end)
  • Next we will pass the parameters to the caster to fire.
resources.fire.OnServerEvent:Connect(function(plr, origin, direction, speed)
	caster:Fire(origin, direction, speed)
end)

The gun should shoot but there is a problem it cant shoot when aiming to fix that we will add RaycastParams:

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.IgnoreWater = true

And add it inside the fire event:

params.FilterDescendantsInstances = {plr.Character, ignore} --the player so he cant kill himself and the ignore folder

3.3 Visualization
If you ever played any kind of game you probably noticed that the gun shoots cool glowing bullets right? Well making them is actually pretty easy!

  • Lets start by making the actual bullet:
    I made a part made it anchored and not can collide and also transparent. Next I added two attachments and a trail. After customizing the trail It looks like this:
    https://gyazo.com/1fb70da296ac58c2514e53e1c1308395
  • Now to make it actually appear we will need quite a few lines:
local bullet = resources:WaitForChild("bullet") --first we will find the bullet
  • Now lets make a cast behavior:
local behavior = fastcast.newBehavior()
behavior.RaycastParams = params
behavior.Acceleration = Vector3.new(0, - workspace.Gravity, 0) --natural bullet drop!!!!!! yay
behavior.AutoIgnoreContainer = false
behavior.CosmeticBulletContainer = ignore
behavior.CosmeticBulletTemplate = bullet
--make sure that you also put behavior as a parameter inside the caster:Fire()
  • Now we will make two functions, the first:
--this works something like splitting the trajectory into segments and then moving the bullet each segment.(you can see the segments with debug visualization)
function onLengthChanged(cast, lastpoint, direction, length, velocity, bullet)
	if bullet then
		wait() --this is needed
		local bulletLength = bullet.Size.Z/2
		local offset = CFrame.new(0,0, - (length - bulletLength))
		bullet.CFrame = CFrame.lookAt(lastpoint, lastpoint + direction):ToWorldSpace(offset)
	end
end
  • And the second function:
--this is just so you know how to check if the bullet hit
function onRayHit(cast, result, velocity, bullet)
	local hit = result.Instance
	local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid:TakeDamage(20)
	end
	debris:AddItem(bullet, 0.1)
end

Make sure the functions are above the fire event!

  • And lets connect the functions:
--below resources.fire:Connect(function()
caster.LengthChanged:Connect(onLengthChanged)
caster.RayHit:Connect(onRayHit)

Thats it if you did everything alright it should work if not you can always check my game code if you made any errors.

Thank you to everyone who reads these without you I wouldn’t be able to make these and watch out cuz in the next one we will do reloading!

Part1: FPS Tutorial [Part 1]
Part2: FPS Tutorial [Part 2]
The game: https://www.roblox.com/games/6315497609/Tutorial-FPS-part-3

Thank you and good bye!

21 Likes

Working on the fourth part. Its probably gonna come out on the weekend.

5 Likes

Thanks! These tutorials are great!

4 Likes

I wish this used animations instead of CFrame, but alas. Good tutorial.

I am doing the same with my current fps game it make animating hard but it makes it much more customizable

When is the 4th part gonna come out? :smiley:

Working on it, took a break from FPS. Dont worry I will finish these series, I actually have written a little bit of the part 4 a long time ago.

1 Like

Good, Work! on that project its very well done! hope you keep up the good work!

Thanks dude, taking a break while working on a knock off H3VR on roblox lol, gonna be back soon though.

Np! Enjoy your break, And we will fill in for you on other developer work, Sometimes’ I’m busy answering questions, And stuff.

FPS Tutorial Part 4 is out now, I am very sorry for the long wait - LINK.