Collision group and CFrame problem while creating a bullet

I have been creating an FPS game. Next step was to create a working bullet. So simply, I fired an event every time player clicks and sent a cframe of a gun muzzle part (from where the bullet should fire) and set the bullet’s cframe to the muzzle’s cframe.

The problem is that the server doesn’t set the bullet cframe to the muzzle cframe.

I played around with the collision group for the view model and the character and when I change collision group settings it works but the character falls trough the baseplate. Also when I start the game and change afterwards to the server side and return back to the client side it suddenly works (I published the place but there also doesn’t work)

  • local script
local weapons = replicatedStorage:WaitForChild("Weapons")

local player = game.Players.LocalPlayer
repeat wait() until player.Character
local char = player.Character
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local viewModel = repViewModel:Clone()
viewModel.Parent = camera

mouse.Icon = "rbxassetid://138865017"
player.CameraMode = Enum.CameraMode.LockFirstPerson

for _, v in pairs(viewModel:GetChildren()) do
	if v:IsA("BasePart") then
		physicsService:SetPartCollisionGroup(v, "NoCollisionViewModel")
	end
end

for _, part in pairs(char:GetChildren()) do
	if part:IsA("BasePart") then
		physicsService:SetPartCollisionGroup(part, "NoCollisionViewModel") 
	end
end

local function giveWeapon(repWeapon)
	for _, weapon in pairs(viewModel.Weapons:GetChildren()) do
		weapon:Destroy()
	end
	
	local weapon = repWeapon:Clone()
	weapon.Parent = viewModel.Weapons
	
	local joint

	if not viewModel.Head:FindFirstChild("Motor6D") then
		joint = Instance.new("Motor6D")
	else
		joint = viewModel.Head.Motor6D
	end

	joint.C0 = CFrame.new(weapon.C0.Value)
	joint.Part0 = viewModel.Head
	joint.Part1 = weapon.Handle
	joint.Parent = viewModel.Head
end

giveWeapon(weapons:WaitForChild("MP-40"))

runService.RenderStepped:Connect(function()
	viewModel.Head.CFrame = camera.CFrame
end)

mouse.Button1Down:Connect(function()
	local shoot
	local cf
	local weapon = viewModel.Weapons:FindFirstChildWhichIsA("Model")

	for _, v in pairs(weapon:GetDescendants()) do
		if v:IsA("BasePart") then
			if v.Name == "Shoot" then
				shoot = v
			end
		end
	end
	
	cf = shoot.CFrame
	
	shootEvent:FireServer(mouse.Hit, cf)
end)
  • server script
shootEvent.OnServerEvent:Connect(function(player, target, cf)
	local char = player.Character
	local weapon = char.Weapons:FindFirstChildWhichIsA("Model")
	
	local shoot
	
	if weapon then
		for _, v in pairs(weapon:GetDescendants()) do
			if v:IsA("BasePart") then
				if v.Name == "Shoot" then
					shoot = v
				end
			end
		end
	end
	
	
	if shoot then
		local bullet = Instance.new("Part")
		bullet.Name = "Bullet"
		bullet.Material = "Neon"
		bullet.BrickColor = BrickColor.new("Gold")
		bullet.CFrame = cf
		bullet.Anchored = false
		bullet.CanCollide = false
		bullet.Size = Vector3.new(5, 0.2, 0.2)
		
		local speed = 100
		
		local velocity = Instance.new("BodyVelocity")
		velocity.Velocity = target.LookVector * speed
		velocity.Parent = bullet
		
		bullet.Parent = workspace.Bullets
	end
end)

I tried creating a bullet on a client side but it also doesn’t set the part cframe to the muzzel cframe, the same happends when i switch between server and client side except when creating on client it looks smoother.

1 Like

I see you’re using bodymovers and creating the bullet on the server.

Do you think this could be a network ownership issue?

I tried setting network owner to server and to player but it didn’t change anything except the smoothness of a bullet.

I noticed that the bullets spawn somewhere below my character. Here is an image of bullets spawning below my character (you can see the path of bullets when I was walking backwards and shooting, they still have the correct lookVector but a wrong spawning position)

Try create a part at the position you’re initially trying to teleport, could possibly give insight.

just wondering, why are you using the

target

's lookvector? shouldn’t it be the muzzle part?

It has the same result, I tried with both and didn’t really see any difference, but that does not effect this problem.

I just deleted the velocity of a bullet and it has the right X and Z axis (half a stud difference) but the Y axis is off, I don’t know why. Also instead of setting cframe of the client’s muzzle part, I sat cframe of the server’s muzzle part and it worked except the client and server muzzle are not at the same position (height) because the weapons have different sizes for better looks.

The point is that is spawns, so the problem is client’s cframe.

I just had to anchor the torso of my view model :man_facepalming:
Thanks for helping me anyways!