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

How can I make a smooth projectile that contains two or more welded parts?

Asked by
asadefa 55
5 years ago
Edited 5 years ago

I am trying to make a projectile (Energy ball) for my game. I am using body velocities, but the issue is is that it seems laggy. I shoot it then half a second through flight it seems to stop in mid air for a quarter of a second then continue. This only happens the first minute of playing. I tested shooting at the ground so at an angle so it will go slower, but it seems the the ball freezes when it reaches a certain distance from the player, not a certain time.

The actual projectile is a less transparent ball inside of a bigger, more transparent ball, connected by a weld. If I remove one of the balls so the projectile is just one ball, my issue is gone. How can I remove the issue while keeping both balls?

Here is my code that I tried:

EnergyBallEvent.OnServerEvent:Connect(function(Player, MouseClickLocation)
    local Character = Player.Character
    local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
    if Humanoid.Health > 0 and not DisabledItems[Player.UserId..'EnergyBall'] then
        DisabledItems[Player.UserId..'EnergyBall'] = true
        Humanoid:LoadAnimation(script.EnergyBallAnimation):Play()
        local Ball = Instance.new("Part", SpellsFolder)
        local CasterValue = Instance.new("ObjectValue", Ball)
        CasterValue.Name = "Caster"
        CasterValue.Value = Player.Character
        Ball:SetNetworkOwner(nil) --removing or modifying this
        Ball.Transparency = 1
        Ball.Shape = Enum.PartType.Ball
        Ball.BrickColor = BrickColor.new(0, 255, 255)
        Ball.Material = Enum.Material.Neon
        Ball.Size = Vector3.new(2.2, 2.2, 2.2)
        Ball.Massless = true
        Ball.CanCollide = false
        local Weld = Instance.new("Weld", Ball)
        Weld.Part0 = Ball
        Ball = Instance.new("Part", Ball)
        Ball:SetNetworkOwner(nil) --or this have no effect that is visible
        Ball.Transparency = 1
        Ball.Shape = Enum.PartType.Ball
        Ball.BrickColor = BrickColor.new(0, 255, 255)
        Ball.Material = Enum.Material.Neon
        Ball.Size = Vector3.new(1.6, 1.6, 1.6)
        Ball.Massless = true
        Weld.Part1 = Ball
        Weld = Instance.new("Weld", Ball)
        Weld.Part0 = Ball
        if Character:FindFirstChild("LeftHand") then
            Weld.Part1 = Character.LeftHand
        else

        end
        for i = 0, 40, 1 do
            Ball.Transparency = Ball.Transparency - 0.012
            Ball.Parent.Transparency = Ball.Parent.Transparency - 0.006
            wait(0.02)
        end
        Weld:Destroy()
        local Motion = Instance.new("BodyVelocity", Ball)
        Motion.Velocity = ((MouseClickLocation - Player.Character.HumanoidRootPart.Position).Unit*100)
        DisabledItems[Player.UserId..'EnergyBall'] = nil
    end
end)

Here is some more code I tried:

nergyBallEvent.OnServerEvent:Connect(function(Player, MouseClickLocation)
    local Character = Player.Character
    local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
    if Humanoid.Health > 0 and not DisabledItems[Player.UserId..'EnergyBall'] then
        DisabledItems[Player.UserId..'EnergyBall'] = true
        Humanoid:LoadAnimation(script.EnergyBallAnimation):Play()
        local Model = Instance.new("Model", SpellsFolder)
        local Ball = Instance.new("Part", Model)
        local CasterValue = Instance.new("ObjectValue", Model)
        CasterValue.Name = "Caster"
        CasterValue.Value = Player.Character
        Ball.Transparency = 1
        Ball.Shape = Enum.PartType.Ball
        Ball.BrickColor = BrickColor.new(0, 255, 255)
        Ball.Material = Enum.Material.Neon
        Ball.Size = Vector3.new(2.2, 2.2, 2.2)
        Ball.Massless = true
        Ball.CanCollide = false
        local Weld = Instance.new("Weld", Ball)
        Weld.Part0 = Ball
        local Ball2 = Instance.new("Part", Model)
        Model.PrimaryPart = Ball2
        Ball2:SetNetworkOwner(nil)
        Ball2.Transparency = 1
        Ball2.Shape = Enum.PartType.Ball
        Ball2.BrickColor = BrickColor.new(0, 255, 255)
        Ball2.Material = Enum.Material.Neon
        Ball2.Size = Vector3.new(1.6, 1.6, 1.6)
        Ball2.Massless = true
        Weld.Part1 = Ball2
        Weld = Instance.new("Weld", Ball2)
        Weld.Part0 = Ball2
        if Character:FindFirstChild("LeftHand") then
            Weld.Part1 = Character.LeftHand
        else

        end
        for i = 0, 40, 1 do
            Ball2.Transparency = Ball2.Transparency - 0.012
            Ball.Transparency = Ball.Transparency - 0.006
            wait(0.02)
        end
        Weld:Destroy()
        local Motion = Instance.new("BodyVelocity", Ball)
        Motion.Velocity = ((MouseClickLocation - Player.Character.HumanoidRootPart.Position).Unit*100)
        DisabledItems[Player.UserId..'EnergyBall'] = nil
    end
end)

I hate this behavior and I NEED to fix it

Another observation is if I comment out the second ball creation and stuff so it is a just one ball, the bug is gone

0
If you handle the body velocity / physics on the server there will be some sort of lag and it will happen like you described, it jitters in some sort of way. Transfer all the physics and make the client do the work and you will see that all your physics will be way smoother. iDarkGames 483 — 5y
0
How can I do that? asadefa 55 — 5y
0
Just shoot a remote request to the client in this case for everyone to see it :FireAllClients() and then just do all the part instantiation and physics in the client. It will be smoother and it will replicate to other clients. iDarkGames 483 — 5y
0
what do I call :FireAllClients() on exactly? asadefa 55 — 5y
View all comments (3 more)
0
A Remote Event, it's a method of a RemoteEvent to actually replicate all the changes on all the clients. iDarkGames 483 — 5y
0
So I call it on the RemoteEvent object? asadefa 55 — 5y
0
Where do I fire all clients? Where in the code asadefa 55 — 5y

Answer this question