So I am working on a destruction system and it seems to work pretty well on server side. But when I test the game on client, the structure starts freezing mid air even though there are no frame drops.
I have tried setting the network owner to all of the parts in the structure to nil but it doesn’t seem to change much.
When falling, the whole structure is set to CanCollide = false. On impact the bigger parts are replaced with smaller peaces and welded together using :JoinToOutsiders() and the chunks that are colliding with the ground are Destroyed.
Looks like an issue where the parts network ownership is being set to the nearest client. When that happens (ownership change) there is a little lag spike. To prevent this, set the network owner to the server by passing nil as the parameter. basePart:SetNetworkOwner(nil). This will prevent the assembly from automatically being set to the nearest client
game:GetService("RunService").Heartbeat:Connect(function()
for i,part in pairs(workspace.Debri:GetDescendants()) do
if part:IsA("BasePart") and part:IsGrounded()==false then
part:SetNetworkOwner(nil)
if part.AssemblyLinearVelocity.Magnitude>=impspeed then
local touchingparts = workspace:GetPartsInPart(part)
if #touchingparts~=0 then
local validcollision
for i,v in pairs(touchingparts) do
if (v.AssemblyLinearVelocity-part.AssemblyLinearVelocity).Magnitude>=impspeed then
validcollision=true
if v:IsDescendantOf(workspace.Debri) then
if v.Size.X>fragmentSize or v.Size.Y>fragmentSize or v.Size.Z>fragmentSize then
replacePartWithFragments(v,Vector3.new(fragmentSize,fragmentSize,fragmentSize))
else
game.Debris:AddItem(v,0.05)
end
end
end
end
if validcollision then
if part.Size.X>fragmentSize or part.Size.Y>fragmentSize or part.Size.Z>fragmentSize then
replacePartWithFragments(part,Vector3.new(fragmentSize,fragmentSize,fragmentSize))
else
local totalmass = 0
for i,v in pairs(part:GetConnectedParts(false)) do
totalmass+=v.Mass
end
part:ApplyImpulseAtPosition(part.AssemblyLinearVelocity.Unit*-math.min(150*(1/(0.01*#part:GetConnectedParts(true)))*part.Mass,totalmass*part.AssemblyLinearVelocity.Magnitude), part.Position)
part.AssemblyAngularVelocity/=1.2
part:Destroy()
end
end
end
part.CanCollide=false
else
part.CanCollide=true
end
end
end
end)