Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
5 years ago
Edited 5 years ago

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)
0
Output: Can only call Network Ownership API on a part that is descendent of Workspace GameBoyOtaku 63 — 5y
0
Fixed; forgot that you can only set network ownership when the part exists in the workspace RayCurse 1518 — 5y
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. ozzyDrive 670 — 5y
Ad
Log in to vote
-1
Answered by 5 years ago

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.

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? GameBoyOtaku 63 — 5y
0
nvm I fixed that problem still lags though GameBoyOtaku 63 — 5y

Answer this question