Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my bullet stop for a second then move with body velocity?

Their are two problems actually one: My bullet isn't facing straight when I shoot it for some reason and two my bullet will stop for a second then move. I'm not sure why but

Video Showing the Problem

LocalScript

function createbullet()
	local flash = cam[currentgun.Name][currentgun.Name].Flash.CFrame
	rep.Events.Bullet:FireServer(mouse.hit, flash, p)
end

ServerScript

game.ReplicatedStorage.Events.Bullet.OnServerEvent:connect(function(player, mousehit, flash, p)
	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.CFrame = CFrame.new(flash.p)
	local bv = Instance.new("BodyVelocity", bullet)
	bv.MaxForce = Vector3.new(1e8, 1e8, 1e8)
	bv.Velocity = mousehit.lookVector * 500
	bullet.CanCollide = false
	bullet.Parent = workspace
	game.Debris:AddItem(bullet, 2)

Any help will be appreciated

2 answers

✓ Accepted answer

This problem is most likely caused by the latency between the server and client when the network ownership of the bullet switches from the client to the server (you shouldn't have this problem if accurate play solo is disabled).

The solution is to call SetNetworkOwner() on the bullet with an argument of nil (this means that the physics will be calculated on the server) like so:

--Server Script
game.ReplicatedStorage.Events.Bullet.OnServerEvent:Connect(function(player, mousehit, flash, p)
	local bullet = game.ReplicatedStorage.Bullet:Clone()
	bullet.CFrame = CFrame.new(flash.p)
	local bv = Instance.new("BodyVelocity", bullet)
	bv.MaxForce = Vector3.new(1e8, 1e8, 1e8)
	bv.Velocity = mousehit.lookVector * 500
	bullet.CanCollide = false
	bullet.Parent = workspace

	bullet:SetNetworkOwner(nil)
	game.Debris:AddItem(bullet, 2)
end)

3 comments

0

Output: Can only call Network Ownership API on a part that is descendent of Workspace

0

Fixed; forgot that you can only set network ownership when the part exists in the workspace

1

Letting the server calculate the physics will yield the same delayed looking result for the shooting client. The clients should create their own projectiles and the server its own.

you can get around this issue by not using a body velocity. Instead of a body velocity, you can use the velocity property of base parts.

For example:

local bullet = game.ReplicatedStorage.Bullet:Clone()
bullet.CFrame = flash --assuming flash is a CFrame here
bullet.Velocity = mousehit.lookVector * 500

Now for the future, try not to use the parent argument of Instance.new as it causes some performance issues. Also try to steer away from CFrame.p and connect in favor of CFrame.Position and Connect.

2 comments

0

it's still stopping midway but it did stop rotating weirdly thanks for that but now it's shooting 3 bullets rather than one like what the?

0

nvm I fixed that problem still lags though

This discussion is preserved as it was. New answers, comments, and votes are closed.