Cframe expected, got vector3

Im trying to make a gun and im using :ApplyImpulse to do it. When I shoot the gun I get an error in the output that says “CFrame expected, got vector3” when I change it to a CFrame it says “Vector3 expected, got CFrame”
I’ve gotten this problem many times before and this is my first time writing about it.

---ServerScript:
script.Parent.gun_fired.OnServerEvent:Connect(function(plr, mouse_POS)
	print(mouse_POS)
	script.Parent.Parent.SFX.Shot:Play()
	local direction = script.Parent.Parent.SmokePart.Position - CFrame.new(mouse_POS)
	local force = direction * Vector3.new(0, game.Workspace.Gravity / 2, 0)
	local effects = script.Parent.Parent.SmokePart:GetChildren()
	---Effects
    for i, v in pairs(effects) do
		if v.Name == "A1" or "A2" or "BEAM1" or "BEAM2" or "Weld" then
			
		else
			v.Enabled = true
		end
	end
	task.wait(0.3)
	for i, v in pairs(effects) do
		if v.Name == "A1" or "A2" or "BEAM1" or "BEAM2" or "Weld" then

		else
			v.Enabled = false
		end
	end
    -----------------------------
	local bullet = game.ServerStorage.bullet:Clone()
	bullet.Parent = workspace
	bullet.Position = script.Parent.Parent.SmokePart.Position
	bullet:ApplyImpulse(force * bullet.AssemblyMass)

end)
---Localscript:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local equipped = false

script.Parent.Parent.Equipped:Connect(function()
	equipped = true
	script.Parent.Parent.SFX.equip:Play()
	print(player.Name, "Equipped their Glock")
end)

script.Parent.Parent.Unequipped:Connect(function()
	equipped = false
	script.Parent.Parent.SFX.unequip:Play()
	print(player.Name, "unequipped their Glock")
end)

mouse.Button1Down:Connect(function()
	local mouse_POS = mouse.Hit
	script.Parent.gun_fired:FireServer(mouse_POS)
end)

If you have any off topic improvements, feel free to share them in the replies.
I’ve also the noticed the effects aren’t working.

Hey there!

Firstly, I think there’s a little bit of a confusion in your usage of mouse_POS. Checking the docs, we can see that mouse.Hit is actually a CFrame:

Therefore, if you want to send the position, in the local script I would change to:

mouse.Button1Down:Connect(function()
	local mouse_POS = mouse.Hit.p
	script.Parent.gun_fired:FireServer(mouse_POS)
end)

Secondly, in the ServerScript, the direction should be a Vector3. I don’t see why would you convert the mouse_POS into a CFrame. I would change the direction calculation to:

local direction = (script.Parent.Parent.SmokePart.Position - mouse_POS).Unit

Don’t forget, when you want to calculate directions, always normalize the Vector3 using the .Unit. You can read more about why this is done here.

Now, after having the direction, just multiply it by a scalar to get the resulting force:

local force = direction * game.Workspace.Gravity / 2

We are now sure the force impulse is a Vector3, as expected by the :ApplyImpulse() method.

So now everything should work! Let me know if there are other things not working, as I haven’t tested anything written here, so I might have minor mistakes.

The bullets are coming out now, but their doing some really funky stuff. I’m guessing this is because of the mouse.hit.p because I remember hearing that its not a reliable way of doing these types of things. For some reason I cant upload the video so I’m just going to explain in further detail. The bullets are refusing to get close to where I shot at. Their going in all of these directions.

Nevermind I fixed it. It was with the direction. I just swapped the positions.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.