BodyVelocity is laggy when played on server

  1. What do you want to achieve? Keep it simple and clear!
    I want the ball to move smoothly whenever I apply force on it.

  2. What is the issue? Include screenshots / videos if possible!
    Well, It`s pretty laggy.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked for solutions, on the Dev Hub, but they all said to set the network owner to the player, but it will still be laggy for others and it’s really easy to exploit.

Here is my code :

	hitbox:SetNetworkOwner(player)
	hitbox.Parent.Ball:SetNetworkOwner(player)
	local lv = Instance.new("BodyVelocity", hitbox.Parent.Ball)
	lv.MaxForce = Vector3.new(10000,10000,10000)
	
	if side == "Left" then
		if players:GetPlayerFromCharacter(player.Character) and player.Character:FindFirstChild("HumanoidRootPart") then 
			if player.Character.HumanoidRootPart:FindFirstChild("BallWeld") then
				player.Character.HumanoidRootPart.BallWeld:Destroy()
			end
			
			lv.Velocity = -hitbox.CFrame.RightVector * 50
		end
	else
		if players:GetPlayerFromCharacter(player.Character) and player.Character:FindFirstChild("HumanoidRootPart") then 
			if player.Character.HumanoidRootPart:FindFirstChild("BallWeld") then
				player.Character.HumanoidRootPart.BallWeld:Destroy()
			end
			
			lv.Velocity = hitbox.CFrame.RightVector * 50
		end
	end
	game.Debris:AddItem(lv, .1)
	
	wait(.5)
	hitbox:SetNetworkOwner(nil)
	hitbox.Parent.Ball:SetNetworkOwner(nil)

Anyways, Thank to anyone who can help me resolve this problem!

2 Likes

You’re setting the NetworkOwner to the player.

Also BodyVelocity is deprectated. You should use VectorForce instead

2 Likes

Ok, thank you! But I set the networkOwner of 2 different part.

Sorry I misread lol, I removed that comment.
But yeah, using BodyMovers on parts that are Network Owned by a Client will cause lag. You should use the BodyMovers locally

Oh ok, but how will the server know that the ball moved?

You can use Part.AssemblyLinearVelocity to get the Velocity, or you can fire a RemoteEvent from Client > Server to let the server know. Though you will have to do some sanity checks to avoid exploits / abuse

2 Likes

Yeah, that’s what I’m worried about, but could you explain the AssemblyLinearVelocity part.

AssemblyLinearVelocity just returns the Parts velocity in a Vector3 value
Eg
if the AssemblyLinearVelocity = Vector3.new(0,0,5) then it is moving 5 studs in the Z axis.

Why does the server need to know the ball moved?

Oh that’s cool. But how could I use it? Sorry for so much question.

Because There is an hitbox on the ball. It’s mainly for animations.
image

1 Like

so I need to set the hit box to same place has the ball

You can just use WeldConstraints to weld the hitbox to the part
Or use AlignPosition to move the hitbox to the Ball’s position

Or simply

Hitbox.Position = Ball.Position

yeah but the function when a player touches the ball is on a server script so it uses the server hitbox position

If you just make the ball owned by the Server SetNetworkOwner(nil) then you can add a touched event in a server script and use Ball:ApplyImpulse( Vector3 ) to apply a force to the ball in the desired direction
EG:

Ball.Touched:connect(function(part)

local LookAt = CFrame.lookat(part.Position, ball.Position)
local Direction = LookAt.LookVector
local Force = 500

Ball:ApplyImpulse(Direction*Force)
end)
1 Like

Hmm, that’s interesting, let me try.

1 Like

alright Ty! Tho it won’t fix my laggy problem, ima try to fix it by puting it locally.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.