I have a system that picks up collision between balls and bounces the balls accordingly that works as intended. However, its performance is very laggy and I find that the collision does not occur straight away. I am stuck for a way to improve the script.
I had to dig into Network Ownership and base the movements off of it, but I had to leave collision to the Server Side.
Here is attachment of what the delay looks like (my pc is dogwater so lag is apparent)
robloxapp-20220421-2244029.wmv (810.1 KB)
The script features a server script and a module. Here it is:
--ModuleScript
local module = {}
module.Check = function()
local table = {}
local u = workspace.BallFolder:GetChildren()
for i,v in pairs(u) do
for i2,v2 in pairs(u) do
if v~=v2 then
--v and v2 are different balls
local magnitude = (v.Position - v2.Position).Magnitude
if magnitude<=9.25 then
local e = true
for c,collisionPair in pairs(t) do
if not ((collisionPair[1]~= v and collisionPair[2]~=b) or (collisionPair[1]~=b and collisionPair[2]~=v)) then
e = false
end
end
--v and v2 are *touching*
if e then table.insert(t,{v,v2}) end
end
end
end
end
return table
end
module.Render = function(a)
local t = a
for i,v in pairs(t) do
for i2,x in pairs(t) do
if btonumber(v[1]==x[1])~=1 then
if v[1]==x[2] and v[2]==x[1] then
print("Collision Detected!")
local directionVector = (v[2].CFrame.p - v[1].Position).unit
local directionVector2 = (v[1].CFrame.p - v[2].Position).unit
local vel1 = directionVector
local vel2 = directionVector2
local c1 = 1
local c2 = 1
for i,v in pairs(v[1]["Effects"]:GetChildren()) do
if v.Name=="Balloon" then
c1 = c1+ .5
end
end
for i,v in pairs(v[2]["Effects"]:GetChildren()) do
if v.Name=="Balloon" then
c2 = c2+.5
end
end
local v1Vel = v[1].Velocity
local p1 = game.Players[v[1].Name]
local p2 = game.Players[v[2].Name]
RS.Control.CollisionReturn:FireClient(p1,(vel2)*math.abs( v[2].Velocity.Magnitude*b*c1/c2))
RS.Control.CollisionReturn:FireClient(p2,(vel1)*math.abs(v1Vel.Magnitude*b*c2/c1))
RS.Anim.Bump:FireClient(p1)
RS.Anim.Bump:FireClient(p2)
p1.Stats.Adrenaline.Value = p1.Stats.Adrenaline.Value+5
p2.Stats.Adrenaline.Value = p2.Stats.Adrenaline.Value+5
table.remove(t,i)
table.remove(t,i2)
local collideEffect = game.ReplicatedStorage.Collide:Clone()
collideEffect.CFrame = CFrame.new((v[1].Position+v[2].Position)/2,workspace.CamPart.CFrame.Position)
collideEffect.Parent = workspace
end
end
end
return
end
return module
-- Script
local collision = {}
local cModule = game.ReplicatedStorage.CollisionModule
while task.wait() do
table.clear(collision)
collision = cModule.Check()
cModule.Render(collision)
end
Any help is appreciated. Thanks.
Iām really stuck