I want to create a football ball using linear velocity (I tried force and it makes the ball direction really buggy). I used the code below and the direction isn’t buggy anymore but the ball is now extremely laggy. Any solutions to make the ball better?
local ball = script.Parent
wait(1)
ball:SetNetworkOwner(nil)
local Debris = game:GetService("Debris")
function onTouched(otherPart)
if otherPart.Name == "HitBox" then
local hitbox = otherPart
local Char = otherPart.Parent
local mode = Char.Mode.Value
local punch = script.Parent.Punch.Value * 2
local HRP = Char.HumanoidRootPart
if mode == 1 then
local Attachment = Instance.new("Attachment")
local LV = Instance.new("LinearVelocity")
LV.MaxForce = 1e9
LV.VectorVelocity = HRP.CFrame.lookVector * punch
LV.Attachment0 = Attachment
Attachment.Parent = script.Parent
LV.Parent = script.Parent
Debris:AddItem(LV, 1)
Debris:AddItem(Attachment, 1)
First off, is there any reason why you’re creating a new Attachment instance to Ball (script.Parent) in the code instead of adding it to the Ball to begin with? The code will be much cleaner and there would less work for the computer to do, which could help with the lag a bit.
Also, I recommend adding a debouncer, as the problem could be that, due to how finicky Touched is, could end up creating multiple LinearVelocity instances as well as Attachment instances, which could also contribute to the lag.
-- Make sure you add a "Attachment" (called "BallAttachment") instance as a child of "Ball" (script.Parent)
local Ball = script.Parent
-- "debounced" is used to prevent something from being triggered twice unintentionally (which can help with lag)
local debounced = false
function onTouched(otherPart)
-- Do not use abbreviations or acronyms (unless if they are well-known, like PNG)
-- as you are making it difficult on yourself (especially your future self, who would forget
-- about the code after a few weeks) on what is what and why those variables exist.
if (otherPart.Name == "HitBox") then
local Character = otherPart.Parent
local Mode = Character.Mode.Value
local Punch = 2 * Ball.Punch.Value
local HumanoidRootPart = Character.HumanoidRootPart
if (Mode == 1) and not (debounced) then
debounced = true
local LinearVelocity = Instance.new("LinearVelocity")
LinearVelocity.MaxForce = 1e9
LinearVelocity.VectorVelocity = Punch * HumanoidRootPart.CFrame.lookVector
LinearVelocity.Attachment0 = Ball.BallAttachment
Debris:AddItem(LinearVelocity, 1)
-- ... (I am assuming there is more code after this point).
debounced = false
end
-- ... (I am assuming there is more code after this point).
Hopefully this will help, and I will be glad to help if you still have issues.
the ball is still really laggy, if you don’t have context already I’m making a hitbox that appears temporarily in front of a player when they press mouse 1, it will then “kick” the ball in the direction they’re facing using linear velocity, but I think there may be a more effective way to make the ball
Try setting the network owner of the ball to the player:
ball:SetNetworkOwner(player)
Roblox switches between players who are rendering the physics in your game, so this should always make the ball’s physics rendered the player kicking the football.
If there are no errors set the parent of the ball to workspace
local ball = script.Parent
wait(1)
local Debris = game:GetService("Debris")
function onTouched(otherPart)
if otherPart.Name == "HitBox" then
local hitbox = otherPart
local Char = otherPart.Parent
local mode = Char.Mode.Value
local punch = script.Parent.Punch.Value * 2
local HRP = Char.HumanoidRootPart
local player = game.Players[otherPart.Parent.Name] -- Defining the player
ball:SetNetworkOwner(player) -- Setting the ball network owner
ball.Parent = workspace -- Parenting the ball back to workspace
if mode == 1 then
local Attachment = Instance.new("Attachment")
local LV = Instance.new("LinearVelocity")
LV.MaxForce = 1e9
LV.VectorVelocity = HRP.CFrame.lookVector * punch
LV.Attachment0 = Attachment
Attachment.Parent = script.Parent
LV.Parent = script.Parent
Debris:AddItem(LV, 1)
Debris:AddItem(Attachment, 1)