I made a shotgun and the gun worked perfectly fine in third person:
But when i went into first person mode, only one bullet is shot:
I opened a local server with 2 players to see what it would look like on the other players side, and sure enough the other client only saw one bullet coming out of the gun. I dont know whats causing this, but im thinking its because of my body following the mouse, even if that is the case, i dont know how to fix it. I sampled that code from UristMcSparks Pistol model.
Here is my code:
for i = 1,7 do
local source = handle.BulletSpawn.WorldPosition
local direction = mouse.Hit.p
local distance = (source - direction).magnitude
local xac = math.random(-5,5)
local yac = math.random(-3,5)
local zac = math.random(-2,5)
local offset = Vector3.new(
xac,
yac,
zac
) * distance / stats.Accuracy -- the more the accurate
local finaldirection = direction + offset
local bullet = makebullet() -- bullet is parented to workspace in this function
bullet.CFrame = CFrame.new(source, finaldirection)
local bv = Instance.new("BodyVelocity")
bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bv.Velocity = bullet.CFrame.LookVector * stats.BulletSpeed
bv.Parent = bullet
local sound = wep.Gun.Hit:Clone()
sound.Parent = bullet
sound.PlayOnRemove = true
end
Any help would be appreciated, thank you.
Interesting issue indeed, though I can’t quickly solve your issue I can however strongly recommend you to use raycasting instead of creating 7 parts at high velocity.
I also believe you should adjust your code to Filtering Enabled since currently you seem to create the bullets for the client only (other players won’t see the bullets)
I already made my gun filtering enabled friendly. I made it fire a Remote Event and that remote event will fire another remote event for all clients (except for the player who shot the gun) and display bullets for them.
Im not really good at raycasting, theres not really a good tutorial on Youtube for it. That’s why im using these types of bullets for now.
1 Like
the other client saw the bullets. this is not a filtering issue
Raycasting is simple. All you need is to make a Ray and use one of the many raycasting functions available on the Workspace.
You have a thing called BulletSpawn, right? This doesn’t account for bullet spread, but you can still use some other CFrame instead of handle.CFrame
to get vectors. Like multiply handle.CFrame
by CFrame.Angles something and use that.
local ray = Ray.new(handle.BulletSpawn.WorldPosition, handle.CFrame.LookVector * 500)
local part, hitPosition = workspace:FindPartOnRayWithIgnoreList(ray, handle.Parent, false, true)
...
Something along those lines. Also keep in mind sometimes the LookVector isn’t what you want (it always comes out the front of the handle, as in the front side of the part, not the front of the model).
(Also, rays are invisible, so you should make the parts tooo…)
Basically: Something something raycasting CFrames lol
Is there any way to make the ray follow the mouses Hit position instead of the handles lookVector?
On top of that, I want to keep the visuals where trails of bullets come out of the gun.
how does arsenal do this its so amazing
local bulletSpawn = handle.BulletSpawn.WorldPosition
local ray = Ray.new(bulletSpawn, (mouse.Hit.Position - bulletSpawn).Unit * 500)
You could do this as well as the bullet visuals.
Or you could make a ray for every bullet, which is more complex, but more realistic. These rays are PERFECTLY ACCURATE. They will ALWAYS hit EXACTLY what the mouse is over. This may not be what you want.
Starting to get it now,
Theres one value im missing, i need the Vector3 of the end point of the Ray to actually know what im doing lol. Is there a way to get that?
Not the end point, the direction of the ray, relative to the origin.
The origin could be the center, and the direction is the, well, direction of each bullet.
Every time you go bullet.CFrame =
, you can save that CFrame and use its LookVector to make a ray like I detailed (ok, glossed over) above. You CAN use the LookVector because the way you’re using the CFrame constructor means that the CFrame’s LookVector is always pointing at the target.
I did this and there seems to be no change, still shoots one in first person:
local source = handle.BulletSpawn.WorldPosition
local ray = Ray.new(source, (mouse.Hit.Position - source).Unit * 500)
local direction = ray.Direction
Oh, I wasn’t really proposing a fix for that issue, sorry. Unfortunately I’m not motivated enough to dig through the script and try to figure out the issue, or code you a complete solution including raycasting and all. Not that you ever asked for it anyway, not trying to be rude or anything.
Its alright! I even learned a bit of raycasting from this process!
1 Like