Gun Projectile is having delays and firing wrong way

Hello,

So my gun projectile is having delays when shooting and it is firing the wrong way when it is firing in front of me (wierdly, this doesn’t happened when it fire in back of me.)

local cooldown = false
script.Parent.RemoteEvent.OnServerEvent:Connect(function(p,a,b)
	if cooldown==false then
		cooldown = true
		script.Parent.yep.Sound:Play()
		local sh = Instance.new("Part",workspace)
		sh:SetNetworkOwner(nil)
		sh.Size=Vector3.new(1,1,5)
		sh.CanCollide = false
		local x,y,z = CFrame.new(script.Parent.yep.Position,a.p):ToOrientation()
		sh.CFrame = CFrame.new(script.Parent.yep.Position)*CFrame.fromEulerAnglesXYZ(x,y,z)
		local bv = Instance.new("BodyVelocity")
		bv.Velocity = Vector3.new(sh.CFrame.lookVector.X,sh.CFrame.lookVector.Y,sh.CFrame.lookVector.Z)*200
		bv.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		bv.P = 500
		bv.Parent = sh
		sh.Transparency = .5
		sh.Material = "Neon"
		sh.BrickColor = BrickColor.new("New Yeller")
		script.Parent.yep.PointLight.Enabled = true
		script.Parent.yep.ParticleEmitter.Enabled = true
		sh.Touched:Connect(function(c)
			if c.Parent:FindFirstChild("Humanoid") then
				if c.Parent.Humanoid.Health<=0 then
					c.Parent.Humanoid.Health = c.Parent.Humanoid.Health-20
					if c.Parent.Humanoid.Health<=0 then
						p.leaderstats.Money.Value = p.leaderstats.Money.Value+50
					end
				end
			end
		end)
		wait(.1)
		script.Parent.yep.PointLight.Enabled = false
		script.Parent.yep.ParticleEmitter.Enabled = false
		game:GetService("Debris"):AddItem(sh,8)
		wait(.9)
		cooldown = false
	end
end)

Here is my script. sh:SetNetWorkOwner(nil) is suppose to make it not delay because I saw that they use it in another post about projectile delay...Aparenty it doesn't work.

- Br, iSyriux

1 Like

You’re calculating your velocity wrong, but I can’t tell you how to do it correctly because you’re variables are named ambiguously. What is p, a, and b?

Hello,

p is Player, a is the mouse Hit cframe, b is mouse Target instance, although not used.

Your velocity should then be

bv.Velocity = (a.Position - p.Character.PrimaryPart.Position).unit * 200

To be 100% accurate, you need to replace the p.Character.PrimaryPart.Position with the spawn position of the part.

1 Like

Hello,


I don’t know if you can see this but the projectile has a slightly wrong rotation.

This happened three times in a row so it’s not my arm moving after the projectile was shot.

Also, there’s still a delay after you clicked a mouse.

1 Like

I’m assuming this is the problem, try using this formula:

bv.Velocity = (Mouse.Hit.P - shootPart).Unit*25 — shootPart is where your bullet will be fired.

Hello,

Unfortunately, @MrNicNac has already gave the exact same response. Please read the posts above before posting.

Sorry, I don’t think I’ll be able to be of more help at this time. The code I provided is mathematically accurate. Here’s me using the same math here.

Here’s the place file that you can use to test it and see if there’s a difference between our code. The only possibility is that one of your positions sent to the remote function is incorrect or the wrong one.

shootDirection.rbxl (21.3 KB)

Hello,

I figured out that I should’ve just done sh.CFrame = CFrame.new(pos,look)

Hello,

How would I fix the bodyvelocity delay problem though? It’s very annoying…?

Hello,

ok nvm i figured it out
Basically I fire all clients on a remoteevent in the serverscript and then the localscript in a player recieves it and handles the part instance velocity (the serverscript still makes one but it’s invisible)

2 Likes