Football (soccer) ball keeps lagging/teleporting

My issue: I have made a soccer ball in my game, but everytime I kick it, it does what it’s supposed to do, but it sometimes stops in place for half a second then continues.

My script:


local Services = {
	Players = game:GetService("Players"),
	Debris = game:GetService("Debris")

}
local Settings = {
	Kick_Cooldown = 0.5,
	Kick_Height = 0.1
}
local Ball = script.Parent
local KickSound = Ball:WaitForChild("Kick")
local IgnoreTable = {}

Ball.Touched:Connect(function(hit)
	local Character = hit.Parent

	local isPlayer = Character:FindFirstChild("Humanoid")

	if not isPlayer then
		return
	end

	if Character then
		if isPlayer then
			if hit.Parent.Name == "GoalkeeperRed" then return end
			if hit.Parent.Name == "GoalkeeperBlue" then return end
			if hit.Parent.Name == "Referee" then return end
			script.Parent.Value.Value = hit.Parent
			local Kick_Force = Character.Power.Value
			local Player = Services.Players:GetPlayerFromCharacter(Character)
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			local Root = Character:FindFirstChild("HumanoidRootPart")
			if not Player or not Humanoid or Humanoid.Health <= 0 or not Root or table.find(IgnoreTable, Player) then
				return
			end
			table.insert(IgnoreTable, Player)
			delay(Settings.Kick_Cooldown, function()
				if not Player then
					return
				end
				local Position = table.find(IgnoreTable, Player)
				if not Position then
					return
				end
				table.remove(IgnoreTable, Position)
			end)
			local Direction = Root.CFrame.LookVector
			if Direction.Magnitude < 0.001 then
				return
			end
			local Velocity = Instance.new("BodyVelocity")
			Velocity.Parent = Ball
			Velocity.MaxForce = Vector3.new(1, 1, 1) * math.huge
			Velocity.Velocity = (Direction.Unit * Kick_Force) + Vector3.new(0, Kick_Force *Settings.Kick_Height, 0)
			Services.Debris:AddItem(Velocity, 0.2)
			KickSound:Play()
			Kick_Force = 20
		end
	end
end)
3 Likes

What I believe is happening here is that there becomes a transfer between “Network Ownership”. Network ownership controls who is simulating the physics for the ball (either it’s one of the players on their client or the server itself). When the ball stops in the air and continues, it’s the client’s network ownership transferring to the server which sometimes takes a moment due to the client’s latency.

What should fix this issue is making sure the ball is always owned by the server.

ball:SetNetworkOwner()

What this does is pretty much makes it so the server has ownership of the ball (this is what it does if it’s called with no arguments). By making sure it’s always owned by the server, there won’t ever be a time when the network ownership has to change from the client to the server, fixing the issue with the ball freezing in air sometimes.

Documentation for BasePart:SetNetworkOwner(): BasePart | Documentation - Roblox Creator Hub

This issue is pretty difficult to deal with, especially if you’re using a physics based ball, though assuming that this code for kicking the ball is already running on the server this fix shouldn’t have much difference to how it already behaves. (This is due to there being an input delay when the client tries kicking the ball due to the ping when telling the server to kick the ball)

2 Likes

Thanks for your response, it lags way less after using this in my script. Now I’ll be able to continue my game without it being unplayable. I appreciate it.

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