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.