How can I make Network Ownership smoother with a soccer ball?

Hi Devforum,

I made a script where when you kick or dribble a ball, it changes network ownership of that ball and such.

However, the hit detection is very laggy/unoptimized. I was wondering if anyone could help me fix this inaccuracy with that, I’ll show a video of what I mean.

Here, you can see how Player 1 passed it to Player 2, and Player 2 only landed 1 kick despite the ball going straight through them multiple times. Not only that but with network ownership being buggy, the players seem to “react late”. I will provide a script for you.

This is a server script from a remote event:

if data.Action == "Dribble" then
		ball:SetNetworkOwner()
		ball:SetNetworkOwner(player)

		BallHandlerModule.changeConfiguration(ball, {
			["Posession"] = player.Name
		})

		BallHandlerModule.Dribble(data)
		
	elseif data.Action == "Shoot" then
--		ball:SetNetworkOwner()
		
		BallHandlerModule.changeConfiguration(ball, {
			["Posession"] = ""
		})
		
		BallHandlerModule.Shoot(data)
	end

Now this script is the functions that are passed on the server:

function BallHandlerModule.Dribble(data)
	local ball = data.Ball
	local action = data.Action
	local direction
	
	if data.Direction then
		direction = data.Direction
	end
	
	ball.AssemblyLinearVelocity = Vector3.new()
	ball.AssemblyAngularVelocity = Vector3.new()
	ball.Velocity = Vector3.new(0,0,0)
	
--	print(direction)
	
	if action == "Dribble" then
		if ball:FindFirstChild("BodyVelocity") then
			ball.BodyVelocity.Velocity = (direction * 25) + Vector3.new(0,100,0)
		else
			local bv = Instance.new("BodyVelocity")
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.Velocity = direction * 25
			bv.Parent = ball

			Debris:AddItem(bv, .1)
		end
	end
end

function BallHandlerModule.Shoot(data)
	local cameraLookVector = data.CameraDirection
	local shotPower = data.ShotPower
	local ball = data.Ball
		
	if cameraLookVector and shotPower then
		
		if ball:FindFirstChild("BodyVelocity") then
			ball.BodyVelocity.Velocity = cameraLookVector * shotPower
		else
			local bv = Instance.new("BodyVelocity")
			bv.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
			bv.Velocity = cameraLookVector * shotPower
			bv.Parent = ball

			Debris:AddItem(bv, .1)
		end
	end
end

Help would be much appreciated to make this more optimized!

I am not going to look through your code but an idea that could work is to just use maths and see where the ball is headed and what player is the CLOSEST to it. You could do this by using the dot product between the velocity direction of the ball and that from the player and then using that see what player is the closest then consequently, set the network ownership of the ball to the player.

This could be one hacky way but I don’t know if this is it.

2 Likes

Thank you for your reply,

I tried this, the only problem is that the ball “stutters” whenever transferring ownership. Is there a way to prevent this?

Hmmm… maybe try doing this calculation as soon as the ball gets kicked? Rather than doing it while it moves.