Physics of soccer ball lagging

I need help with my soccer ball lagging, it works fine when I’m testing it alone but when my friend joined the ball started lagging.

Code:


local players = game:GetService("Players")
local de = game:GetService("Debris")
local rs = game:GetService("ReplicatedStorage")

local ballHitbox = script.Parent
local ball = ballHitbox.Parent
local kickSound = ball:WaitForChild("kickSound")
local t = {}
local cd = 1.3

local lastTouched = rs:WaitForChild("lastTouched")

game.Players.PlayerAdded:Connect(function(player)
	local db = player.Name.."_db"
	t[db] = false
end)

ballHitbox.Touched:Connect(function(hit)
	local frontForce = math.random(30,105)
	local upForce = math.random(10,35)

	local h = hit.Parent:FindFirstChild("Humanoid")

	if h then
		local char = hit.Parent
		local lv = char.HumanoidRootPart.CFrame.LookVector
		local uv = char.HumanoidRootPart.CFrame.UpVector
		
		local player = players:GetPlayerFromCharacter(char)
		local ls = player.leaderstats
		local score = ls.Score
		local db = player.Name.."_db"
		
		if not t[db] then
			t[db] = true
			
			lastTouched.Value = player.Name
			score.Value += 10
			
			local bv = Instance.new("BodyVelocity")
			bv.Parent = ball
			bv.MaxForce = Vector3.new(1, 1, 1) * math.huge
			bv.Velocity = lv*frontForce + uv*upForce
			
			de:AddItem(bv,.2)
			kickSound:Play()
			
			task.wait(cd)
			t[db] = false
		end

	end

end)

Use Network Ownership

If you dont know how to use it then check this tutorial out:

ball:SetNetworkOwner(player)
Code
local players = game:GetService("Players")
local de = game:GetService("Debris")
local rs = game:GetService("ReplicatedStorage")
local ballHitbox = script.Parent
local ball = ballHitbox.Parent
local kickSound = ball:WaitForChild("kickSound")
local t = {}
local cd = 1.3
local lastTouched = rs:WaitForChild("lastTouched")

game.Players.PlayerAdded:Connect(function(player)
	local db = player.Name.."_db"
	t[db] = false
end)

ballHitbox.Touched:Connect(function(hit)
	local frontForce = math.random(30,105)
	local upForce = math.random(10,35)
	local h = hit.Parent:FindFirstChild("Humanoid")
	if h then
		local char = hit.Parent
		local lv = char.HumanoidRootPart.CFrame.LookVector
		local uv = char.HumanoidRootPart.CFrame.UpVector

		local player = players:GetPlayerFromCharacter(char)
		local ls = player.leaderstats
		local score = ls.Score
		local db = player.Name.."_db"
		
		ball:SetNetworkOwner(player)

		if not t[db] then
			t[db] = true
			lastTouched.Value = player.Name
			score.Value += 10

			local bv = Instance.new("BodyVelocity")
			bv.Parent = ball
			bv.MaxForce = Vector3.new(1, 1, 1) * math.huge
			bv.Velocity = lv*frontForce + uv*upForce

			de:AddItem(bv,.2)
			kickSound:Play()

			task.wait(cd)
			t[db] = false
		end
	end
end)
1 Like

thanks ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎