If you see in the video below, the bullets of my gun are shot vertically and not horizontally, so as any normal person would, I tried changing the Orientation of the bullet first. Bullets still shot vertically. I saw many people with similar problems but their scripts used .Velocity instead of using RayCast like mine.
I also slowed down the bullets so you can see that they aren’t shooting right.
-- This is a normal Script located in ServerScriptService
-- This is also not the whole script
GunEvent.OnServerEvent:Connect(function(plr, eventtype, tool, startpos, endpos)
if eventtype == "Shoot" then
tool:SetAttribute("Ammo", tool:GetAttribute("Ammo") - 1)
GunEvent:FireClient(plr, "Shoot")
local Char = plr.Character
local Direction = (endpos - startpos).Unit*1000
local RayCastParamss = RaycastParams.new()
RayCastParamss.FilterType = Enum.RaycastFilterType.Exclude
RayCastParamss.FilterDescendantsInstances = {tool:GetDescendants(), Char:GetDescendants()}
local RayCast = workspace:Raycast(startpos, Direction, RayCastParamss)
if RayCast then
local BulletClone = Bullet:Clone()
BulletClone.Parent = game.Workspace
BulletClone.Position = startpos
BulletClone.CFrame = CFrame.new(startpos, startpos + tool.MainTurret.Barrel.CFrame.LookVector)
local Duration = (endpos - startpos).Magnitude / 135
local BulletTween = TS:Create(BulletClone, TweenInfo.new(Duration, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Position = endpos})
BulletTween:Play()
SoundServ.m2_browning_fireend:Play()
BulletTween.Completed:Wait()
BulletClone:Destroy()
if RayCast.Instance.Parent:FindFirstChild("Humanoid") then
RayCast.Instance.Parent.Humanoid:TakeDamage(Dmg)
BulletTween.Completed:Wait()
BulletClone:Destroy()
end
end
elseif eventtype == "Reload" then
tool:SetAttribute("Ammo", tool:GetAttribute("MaxAmmo"))
GunEvent:FireClient(plr, "Reload")
end
end)
You should check which direction is the part’s front face. It’s likely that the mesh it is using does not have the bullet mesh pointing toward the front face of the part.
The main feeling I get is that the turret is controlled locally perhaps by motor6d or CFrames but this change is not replicated to the server hence the bullet direction is wrong.
You should debug this lookvector variable further and see if it is changing as it should be.
Just like @dthecoolest said, it probably isn’t replicated, but since you’re already passing endpos through the remote event you can just use CFrame.lookAt(startpos,endpos) and adjust the rotation in case your Union’s got weird face directions just like @iammr_oofoof said by using * CFrame.Angles(0,0,math.rad(180)) to adjust accordingly
That should be fine I guess.
Also CFrame.Lookat(pos,posToLookAt) is kinda the same as CFrame.new(pos,posToLookAt), you could use CFrame.new if you feel more comfortable with it.