Designing an FPS Framework: Beginner’s guide [PART 2]

most definitely, here

function module.cast(origin, endposition, velocity, player, damage)
	
	--ORIGIN IS A POSITION VARIABLE DONT DO origin.Position JUST DO origin
	
	local Bullet = Instance.new("Part")
	Bullet.Name = player.Name.."s bullet"
	Bullet.Size = Vector3.new(1, 1, 5)
	Bullet.Anchored = true
	Bullet.CanCollide = false
	Bullet.Color = Color3.new(1, 0.901961, 0.137255)
	Bullet.Material = Enum.Material.Neon
	Bullet.Parent = game.Workspace.CosmeticBullets
	
	Bullet.CFrame = CFrame.new(origin, endposition)

	local Loop

	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
		if (Bullet.Position - origin).magnitude > 5000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	end)
	
	local Hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector * velocity * 1.5)

	if Hit then
		
		local BulletHole = Instance.new("Part")
		BulletHole.Parent = Hit.Instance
		BulletHole.CFrame = CFrame.new(endposition, origin)
		BulletHole.Anchored = true
		Bullet.Size = Vector3.new(1, 1, 1)
		
		if Hit.Instance.Parent:FindFirstChild("Humanoid") and damage ~= nil then
			damage:FireServer(Hit.Instance.Parent, 10)
		else
			Loop:Disconnect()
			Bullet:Destroy()
		end
	end	
end
2 Likes

Ah, you have to put the Raycast inside the Loop like this:

local Loop

	Loop = game:GetService("RunService").RenderStepped:Connect(function(dt)
		Bullet.CFrame *= CFrame.new(0, 0, -velocity * (dt * 60))
		if (Bullet.Position - origin).magnitude > 5000 then
			Bullet:Destroy()
			Loop:Disconnect()
		end
	    local Hit = workspace:Raycast(Bullet.Position, Bullet.CFrame.LookVector * velocity * 1.5)

	    if Hit then
		
		local BulletHole = Instance.new("Part")
		BulletHole.Parent = Hit.Instance
		BulletHole.CFrame = CFrame.new(endposition, origin)
		BulletHole.Anchored = true
		Bullet.Size = Vector3.new(1, 1, 1)
		
		if Hit.Instance.Parent:FindFirstChild("Humanoid") and damage ~= nil then
			damage:FireServer(Hit.Instance.Parent, 10)
		else
			Loop:Disconnect()
			Bullet:Destroy()
		end
	    end

	end)
2 Likes

Could you teach us how to implement a aim system ? I´ve tried with offsets but it is not working :frowning:

1 Like

Update: Added Step 6: Aiming because of demand.

4 Likes

I have some problems with my aiming system, using the same code as you did.

gunmodel.GunComponents.Sight.CFrame = gunmodel.GunComponents.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame,game.ReplicatedStorage.Aim.AimValue.Value)
function module.aim(toaim,viewmodel,gun)
	if toaim then
		game:GetService('TweenService'):Create(game.ReplicatedStorage.Aim.AimValue, TweenInfo.new(1),{Value = 1}):Play()
	else
		game:GetService('TweenService'):Create(game.ReplicatedStorage.Aim.AimValue, TweenInfo.new(1),{Value = 0}):Play()
	end
end

Any fixes? Watch this video

3 Likes

You just have to rotate the part so the orientation is correct.

3 Likes

HumanoidRootPart’s Orientation is 0,0,0.

3 Likes

I meant the sight orientation.

4 Likes

My bad, but this is a little too close is there a way to make the arms visible?

4 Likes

Reposition the sight so it is a bit behind than usual

5 Likes

Thanks man, helped me alot keep up with these great tutorials. :smiley:

5 Likes

what value is AimAlpha? a bool, an int?

2 Likes

“AimAlpha” is a Number Value .

3 Likes

Hello,

It appears all the videos are broken for me for both part 1 and 2.

2 Likes

Hey, im not sure why it isnt tweening, ive just started doing the aiming, and I had triple checked this it looks the same as yours, but I’m not sure whats wrong, this is my code.(Also this is just as you start it and get the tween working)
local-

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = true
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		
		mainModule.aim(true, Viewmodel, GunModel)
		
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		IsPlayerHoldingMouse = false
	end
	
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		
		mainModule.aim(false, Viewmodel, GunModel)
		
	end
end)

module-

function module.aim(toaim, viewmodel, gun)
	if toaim then
		game:GetService("TweenService"):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(1), { Value = 1 }):Play()

		gun.Components.Sight.CFrame = gun.Components.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame, game.ReplicatedStorage.Values.AimAlpha.Value)
	else
		game:GetService("TweenService"):Create(game.ReplicatedStorage.Values.AimAlpha, TweenInfo.new(1), { Value = 0 }):Play()
		
		gun.Components.Sight.CFrame = gun.Components.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame, game.ReplicatedStorage.Values.AimAlpha.Value)
	end
end
3 Likes

Alright, that was a mistake on my part. It should be fixed.

4 Likes

For the lerping, you’re supposed to lerp in the .update() like as mentioned:

2 Likes

like this?

function module.update(viewmodel, dt, RecoilSpring, BobbleSpring, SwayingSpring, gun)
	gun.Components.Sight.CFrame = gun.Components.Sight.CFrame:Lerp(viewmodel.HumanoidRootPart.CFrame, game.ReplicatedStorage.Values.AimAlpha.Value)
1 Like

Yeah that seems about correct.

1 Like

And how can you make a play button and when you click on it, you will get a weapon?
And gun choice.
Im bad programmer… :frowning:

2 Likes