Im trying to make a smooth soccer ball kicking experience for my game Mini Cup. However, when the player touches the ball there is a delay. Also when other players touch the ball they look like they are a couple feet away from the ball.
How would I fix this issue?
Here is the code. There are two scripts.
SERVERSCIPT1 INSIDE BALL - SETS BALL ON SERVER
local ball = script.Parent
while true do
task.wait(5)
ball:SetNetworkOwner(nil)
end
SERVERSCIPT2 INSIDE BALL - PERFORMS KICK
local Services = {
Players = game:GetService("Players"),
Debris = game:GetService("Debris")
}
local Settings = {
Kick_Cooldown = 0, -- Ignore this, i am just too lazy to remove.
}
local Ball = script.Parent
local KickSound = Ball:WaitForChild("Kick")
local IgnoreTable = {}
debounce = false
Ball.Touched:Connect(function(Part)
local Character = Part.Parent
if not Character then
return
end
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)
if debounce == false then
debounce = true
local Direction = Root.CFrame.LookVector
--local Direction = CFrame.lookAt(Root.Position, Root.Position).LookVector
local Kick_Force = Humanoid.WalkSpeed
if Direction.Magnitude < 0.001 then
return
end
if Ball:GetAttribute('LastTouch') ~= nil then
if Ball:GetAttribute('LastTouch') ~= Player.Name then
local st = Ball:GetAttribute('LastTouch')
Ball:SetAttribute('LastTouch', Player.Name)
Ball:SetAttribute('SecondTouch', st)
else
end
else
Ball:SetAttribute('LastTouch', Player.Name)
end
local TeamName = Player:GetAttribute('TeamName')
if game.ServerStorage.GameFolder.Standings:FindFirstChild(TeamName) ~= nil and game.ServerStorage.GameFolder.Standings:FindFirstChild(TeamName):GetAttribute("Touches") ~= nil then
game.ServerStorage.GameFolder.Standings:FindFirstChild(TeamName):SetAttribute('Touches', game.ServerStorage.GameFolder.Standings:FindFirstChild(TeamName):GetAttribute('Touches') + 1)
end
local Velocity = Instance.new("BodyVelocity")
Velocity.Parent = Ball
Velocity.MaxForce = Vector3.new(1, 1, 1) * math.huge
Velocity.Velocity = (Direction.Unit * Kick_Force * 1.7) + Vector3.new(0, Kick_Force * .9, 0)
Services.Debris:AddItem(Velocity, 0.2)
KickSound:Play()
wait(1)
debounce = false
end
end)
So as you can see, I have tried setting the balls network owner to the server (I dont know why I made it a loop…), but it still shows that latency.
What can I do to reduce or get rid of this latency?
Thanks!