Designing an FPS Framework: Beginner's guide

So I got an error in the module script, I don’t know why, here is the error:

Screenshot 2021-05-04 143500

Here is the module script so far:



local MainModule = {}

function MainModule.Update(viewmodel, dt)
	viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end

function MainModule.weldgun(gun)
	local Main = gun.GunComponets.Handle
	
	for i,v in ipairs(gun:GetDescendants()) do
		if v:IsA("BasePart") and v ~= Main then
			local newMotor = Instance.new("Motor6D")
			newMotor.Name = v.Name
			newMotor.Part0 = Main
			newMotor.Part1 = v
			newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
			newMotor.Parent = Main
		end
	end
end

function MainModule.equip(viewmodel, gun, hold)
	local GunHandle = gun.GunComponets.Handle
	local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle
	
	gun.Parent = viewmodel
	HRP_Motor6D.Part1 = GunHandle
	
	local Hold = viewmodel.AnimationController:LoadAnimation(hold)
	Hold:Play()
end

function MainModule.cast(gun,endposition,velocity)
	local GunBarrel = gun.GunComponets.Barrel
	
	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(1,1,5)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.Color = Color3.fromRGB(255,255,255)
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = workspace
	
	Bullet.CFrame = CFrame.new(GunBarrel.Position, endposition)
	
	local Loop
	
	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0,0 -velocity * dt)
		if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	end)
end


return MainModule

Here is the line of code that the error goes to:

Bullet.CFrame = CFrame.new(GunBarrel.Position, endposition)

I don’t know why it’s like this, it’s exactly the same in your code.

2 Likes

Hey! Sorry for not responding (i was at school), but make sure your “endposition” is a Vector3.

4 Likes

I guess we live in very different time zones, but I fixed that issue, but the bullet doesn’t spawn at the current position of the barrel, I don’t know why, it doesn’t even spawn at the barrel, it just spawns in the middle of your screen.

2 Likes

Can you make sure that the barrel is in the correct position? If it spawns in the middle of the space that means the barrel is in an incorrect position.

2 Likes

Here is the link to my the topic I made just to figure out this issue: topic

Here is an image of the barrel’s position:

Screenshot 2021-05-05 084841
You can barely see the barrel, and still it’s not in the center of the screen. I made a video of shooting the gun, but the file was too big somehow.

Also I noticed that part 2 doesn’t have aiming and reloading, a part 3 would be amazing.

This is where the bullet spawns also:
Screenshot 2021-05-05 091320

If possible, could you provide an rbxm file of the module script for this part?

2 Likes

A video would be much better for me to understand but for now all I could recommend is to print() the barrel’s position and the camera’s position (since you said the bullet were spawning in the middle of your screen). And also try doing this after the CFrame.new(barrel.Position, endposition):
Bullet.Position = barrel.Position

Regarding about aiming in reloading, I already gave a statement about aiming here and I won’t be doing a reloading tutorial since it’s very simple.

Update: ok so mid way writing this you editted your post and it looks like the bullet is spawning at 0, 0, 0 (correct me if im wrong). but still try the possible solution above

5 Likes

Yeah I don’t know why it spawns at 0,0,0, but it just does. Also, I used 60 velocity, like you did in your gun, but it moves way faster than your gun. Also reloading is already kind of simple, so I could probably do it myself.

1 Like

That’s because I forgot to do dt * 60. I updated the script but I didn’t update the video.

Here is what I said in Part 2:

image

3 Likes

I printed the position of the bullet, spawns at 0,0,0. I did some extra testing, but nothing else important came up.

2 Likes

Do this also, this way we know if the bullet is spawning to actually positioning to the right position.

And also this.

2 Likes

I did print the barrels position, it’s fine. I modified the module to set the bullets position to the barrel, it works, but after 5-7 seconds, the bullet disappears. The bullet’s position isn’t 0,0,0 anymore, even when the bullet doesn’t appear.

2 Likes

Could you make a video showing this or instead make the game public so I can see the problem myself

2 Likes

It’s a local file, I will publish a game in a second…

game link ^

This is the output of me shooting:


There is no bullets.

3 Likes

Try and set the velocity to like 5, 60 may be too fast and also I noticed that the Y axis is going around -600…?

3 Likes

I think they’re spawning underground. When I go into first person, the bullets go away.
Also, if possible, could you provide an rbxm file of the module script for part 1?

2 Likes

When ingame could you check if the gun is welded properly, and also may I see the LocalHandler?

Unfortunately, No. I already provided the source code below. I don’t want people to gain nothing out of my tutorial (also another reason I don’t use code blocks). Sorry!

4 Likes

1:
Screenshot 2021-05-05 102903

Screenshot 2021-05-05 102922

2:

local GunModel = game.ReplicatedStorage:WaitForChild("SR01A1")
local AnimationsFolder = game.ReplicatedStorage:WaitForChild("SR01A1_ANIMS")
local ViewModel = game.ReplicatedStorage:WaitForChild("ViewModel")
local Mouse = game.Players.LocalPlayer:GetMouse()

local MainModule = require(game.ReplicatedStorage:WaitForChild("MainModule"))

ViewModel.Parent = workspace.Camera
MainModule.weldgun(GunModel)

game:GetService("RunService").RenderStepped:Connect(function(dt)
	MainModule.Update(ViewModel, dt)
end)

MainModule.equip(ViewModel,GunModel,AnimationsFolder.SR01A1_ANIM)

local IsPlayerHoldingMouse
local CanFire = true
local FireDelay = 0.1

game:GetService("RunService").Heartbeat:Connect(function(dt)
	if IsPlayerHoldingMouse then
		if CanFire then
			CanFire = false

			MainModule.cast(GunModel, Mouse.Hit.Position, 5)

			wait(FireDelay)
			print(ViewModel.SR01A1.GunComponets.Barrel.Position)
			CanFire = true
		end
	end
end)

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = false
	end
end)

3: That’s alright, it’s just hard to see the module in a small image.

2 Likes

Are you modifying the barrel’s position in another script? because the barrel’s position is sometimes going into the negatives…?

2 Likes

module:



local MainModule = {}

function MainModule.Update(viewmodel, dt)
	viewmodel.HumanoidRootPart.CFrame = workspace.Camera.CFrame
end

function MainModule.weldgun(gun)
	local Main = gun.GunComponets.Handle

	for i,v in ipairs(gun:GetDescendants()) do
		if v:IsA("BasePart") and v ~= Main then
			local newMotor = Instance.new("Motor6D")
			newMotor.Name = v.Name
			newMotor.Part0 = Main
			newMotor.Part1 = v
			newMotor.C0 = newMotor.Part0.CFrame:inverse() * newMotor.Part1.CFrame
			newMotor.Parent = Main
		end
	end
end

function MainModule.equip(viewmodel, gun, hold)
	local GunHandle = gun.GunComponets.Handle
	local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle

	gun.Parent = viewmodel
	HRP_Motor6D.Part1 = GunHandle

	local Hold = viewmodel.AnimationController:LoadAnimation(hold)
	Hold:Play()
end

function MainModule.cast(gun,endposition,velocity)
	local GunBarrel = gun.GunComponets:WaitForChild("Barrel")

	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(1,1,5)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.Color = Color3.new(255,255,255)
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = workspace

	Bullet.CFrame = CFrame.new(Bullet.Position, endposition)
	Bullet.Position = GunBarrel.Position
	print(Bullet.Position)
	
	local Loop

	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 5))
		if (Bullet.Position - GunBarrel.Position).magnitude > 1000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	end)
end


return MainModule

Without Bullet.Position = GunBarrel.Position the bullet spawns at 0,0,0

2 Likes

Hm that’s weird could you try replacing your gun with my gun that I used in the tutorial:

groza.rbxm (8.7 KB)

You could also remove this Bullet.Position = GunBarrel.Position when testing with the gun I gave you

2 Likes