Help with cframe

  1. What do you want to achieve?
    my aiming dosnt work

  2. What is the issue?
    it shows aimcf is not a valid member of Model "Workspace.Camera.Kar98K"

this is the script

local tool = script.Parent

local vm = game.ReplicatedStorage.Viewmodels.Kar98K:Clone()

local player = game.Players.LocalPlayer
local char = player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera

local run = game:GetService("RunService")

local SwayEffect = 1
local SwayCFrame = CFrame.new()
local LastCameraCFrame = CFrame.new()

local Walk = false
local WalkCFrame = CFrame.new()
local speed = hum.WalkSpeed/5

--sway and bobbing--
run.RenderStepped:Connect(function()
	local Rotation = workspace.CurrentCamera.CFrame:toObjectSpace(LastCameraCFrame)
	local X,Y,Z = Rotation:ToOrientation()
	SwayCFrame = SwayCFrame:Lerp(CFrame.Angles(math.sin(X) * SwayEffect, math.sin(Y)* SwayEffect, 0), 0.25)
	LastCameraCFrame = workspace.CurrentCamera.CFrame

	vm:SetPrimaryPartCFrame(workspace.Camera.CFrame * WalkCFrame * CFrame.new(0,0,0) * SwayCFrame)
	if Walk then
		WalkCFrame = WalkCFrame:lerp(CFrame.new(0.03 * math.sin(tick() * (2.3 * speed)),0.03 * math.cos(tick() * (3 * speed)),0)*CFrame.Angles(0,0,-.02 * math.sin(tick() * (3 * speed))),.5)
	else
		WalkCFrame = WalkCFrame:lerp(CFrame.new(),.1)
	end
end)

--check if the player is walking/running--
hum.Running:Connect(function(Speed)
	if Speed >= 1 then
		Walk = true
	else
		Walk = false
	end
end)

--enable the viewmodel via equip--
tool.Equipped:Connect(function()
	vm.Parent = workspace.Camera
end)

--disable the viewmodel via unequip
tool.Unequipped:Connect(function()
	vm.Parent = game.Debris
end)

--disable the viewmodel via death--
hum.Died:Connect(function()
	vm.Parent = game.Debris
end)

local UIS = game:GetService('UserInputService')
local Character = player.Character

UIS.InputBegan:connect(function(input)--When a player has pressed LeftShift it will play the animation and it will set the normal walking speed (16) to 35.
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 24
		sprint = true
		local animationController = vm:WaitForChild("Controller")
		local animation = vm.Sprint
		local animationTrack = animationController:LoadAnimation(animation)
		animationTrack:Play()
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		Character.Humanoid.WalkSpeed = 16
		sprint = false
		local animationController = vm:WaitForChild("Controller")
		local animation = vm.SprintEnd
		local bye = animationController:LoadAnimation(animation)
		bye:Play()
	end
end)

local maincf = CFrame.new()
local aimcf = CFrame.new()
local aiming = false

run.RenderStepped:Connect(function()
	mouse.TargetFilter = cam

	if vm then
		vm:SetPrimaryPartCFrame(
			cam.CFrame
				* maincf
				* aimcf
		)

		if aiming then
			aiming = true
			aimcf = aimcf:lerp(vm.aimcf, 0.1)
		else
			aiming = false
			aimcf = aimcf:lerp(CFrame.new(), 0.1)
		end
	end
end)

mouse.Button2Down:Connect(function()
	aiming = true
end)

mouse.Button2Up:Connect(function()
	aiming = false
end)

Since it’s not an instance, and is a cframe this is most likely a problem with how you have written this line:

Also, Model | Roblox Creator Documentation has been superseded by PVInstance | Roblox Creator Documentation. You can write it like this instead.

vm:PivotTo(cam.CFrame * maincf * aimcf)
1 Like

it dosnt work :frowning: it says this in output aimcf is not a valid member of Model "Workspace.Camera.Kar98K"

I found your problem. Truthfully I should’ve found it from the beginning. This is the line causing the error.

You can try replacing it with this:

aimcf = aimcf:Lerp(vm:GetPivot(), 0.1)
1 Like

it dosnt shows any error and its not moving

I sort of just assumed that it’d be the cframe of the vm. What CFrame are you trying to lerp it with?

Also, no offense, but I am genuinely confused by this code:

Why are you setting the aiming in the loop when you don’t need to? It never seems like you actually change the value of maincf. I can’t help you any further unless you give me more information for what you are trying to do.

can we discuss this in discord?add me IMPOSTOR#4929

You can just dm me. Click on my profile picture and then click message.

1 Like

For anyone who comes across this topic, basically what @LAZY_DAISY001 was trying to do was create an FPS (First Person Shooter) game. He was having trouble with having the viewmodel of the gun aim hold up to the eye when aimed. If you yourself are trying to create an FPS game, or a game that has FPS elements, I strongly recommend checking out @EgoMoose’s tutorial for the FPS element.

1 Like