Designing an FPS Framework: Beginner's guide

Wait never mind I figured it out.

I have another issue. When firing the gun the projectiles spawn very high up.

Code:

function Module.Fire(Gun, EndPos, Velocity)
	local Origin = Gun.Main.Barrel
	local Bullet = Instance.new("Part")
	Bullet.Size = Vector3.new(10,10,10)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.BrickColor = BrickColor.new("New Yeller")
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = workspace

	Bullet.CFrame = CFrame.new(Origin.Position, EndPos)

	local Loop

	Loop = RS.RenderStepped:Connect(function(DT)
		Bullet.CFrame *= CFrame.new(0,0, -Velocity * (DT * 10 ))
		if (Bullet.Position - Origin.Position).magnitude > 5000 then
			Loop:Disconnect()
			Bullet:Destroy()
		end
	end)
end

Make sure your barrel is in the right position. You can check the barrel position is by using the explorer ingame.

Okay so I checked the position of the barrel and the whole model and somehow it’s super far away. But, it shows on my screen as being right in front of me. I’m not sure what causes this.

Okay I figured it out. I guess I am dumb because I spent at least an hour trying to solve this. But, the problem was the viewmodel wasn’t anchored.

EDIT: Animations don’t work when anchored. So I guess I still need help.
EDIT EDIT: Turns out you can solve this issue by making only the humanoid root part anchored.

4 Likes

Maybe this can be a mistake with the welding? I’ve had this issue before but unfortunately I couldn’t remember what I did to fix it.

Edit: I didn’t see the edit you made, already wrote the post 30 minutes ago forgot to send lol

1 Like

A Bit of topic but what colors do you use for your studio scripts?

This is terrific, I love this work, keep up the good work

I’m actually using vscode, but the theme is Palenight.

If you’re talking about roblox studio theme here:

3 Likes

Ok so I tried setting the Part0 to the humanoid root part and Part1 to the left arm, but whenever I do that it just sets the humanoid root part position to the left arm. Which completely screws up the look of the view model.

I don’t know if this is supposed to happen but it’s really irritating me.

https://cdn.discordapp.com/attachments/842630054189203457/857800968565030972/c6158bb05592624c4d22e638a6c35257.mp4

This is a common thing. To not mess up you either have to use the command bar and inverse the CFrame or just use a plugin. I use this plugin to rig models. RigEdit Lite - Roblox

Plugin really helped, thank you.

Mine as well, but i fixed it by anchoring humanoidrootpart and all of it was working fime

I am having an issue where the gun is not in front of my face. It in fact on the far left of the viewmodel, and it also is off the screen. However, it still moves with it, which makes me think that for some reason there is a huge offset with the viewmodel and the gun’s handle. I don’t know why this is happening.

Local handler script

local gunModel = game.ReplicatedStorage:WaitForChild("A-47"):Clone()
local viewModel = game.ReplicatedStorage:WaitForChild("Viewmodel"):Clone()

local mainModule = require(game.ReplicatedStorage.MainModule)

viewModel.Parent = game.Workspace.Camera

mainModule.weldgun(gunModel)
mainModule.equip(viewModel, gunModel)

game:GetService("RunService").RenderStepped:Connect(function(dt)
	mainModule.update(viewModel, dt)
end)

Main Module script

local module = {}

function module.weldgun(gun)
	local main = gun.GunComponents.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 module.equip(viewmodel, gun)
	local gunHandle = gun.GunComponents.Handle
	local HRP_Motor6D = viewmodel:WaitForChild("HumanoidRootPart").Handle
	
	gun.Parent = viewmodel
	HRP_Motor6D.Part1 = gunHandle
end

function module.update(viewmodel, dt)
	viewmodel.HumanoidRootPart.CFrame = game.Workspace.Camera.CFrame
end

return module

The Motor6Ds all seem to be connected properly.

Make sure the HumanoidRootPart is anchored.

2 Likes

Thanks! Also, for people reading this, if you cannot see your gun even after you have applied any fixes make sure it is actually pointing forwards, as it can be facing the side and thus it appears that it is not attached to your humanoid root part correctly.

I don’t understand this line of code:

luaNewMotor.C0 = NewMotor.Part0.CFrame:inverse() * NewMotor.Part1.CFrame

After some googling I still do not properly understand what the C0 property of a motor6D is, and why was the inverse function used in this case?

I’m new to motor6D lol

C0 means the Part0’s offset. We need to inverse the CFrame fix the position of the part’s CFrame.

I still dont understand how the inverse function is used to ‘fix’ the position of the part’s (what is ‘part’s’? Is it C0 or C1) CFrame, and why do you multiply it by Part1.CFrame

I looked at this post: What is ```CFrame:Inverse()```?
Are you basically subtracting one CFrame from another? What is the purpose of this?