Skip to content

Instantly share code, notes, and snippets.

@richie0866
Created November 6, 2020 05:14
Show Gist options
  • Save richie0866/152b1491856bdca1bdc89d2ff0bfe871 to your computer and use it in GitHub Desktop.
Save richie0866/152b1491856bdca1bdc89d2ff0bfe871 to your computer and use it in GitHub Desktop.
Useful functions for maintaining yo network ownership
-- Simulation
-- 0866
-- October 14, 2020
local Simulation = {}
local physics = settings():GetService("PhysicsSettings")
local client = game:GetService("Players").LocalPlayer
local connection
local sethiddenproperty, setsimulationradius do
if (game:GetService("RunService"):IsStudio()) then
function sethiddenproperty(t, i, v)
t[i] = v
end
function setsimulationradius(radius, maxRadius)
if (maxRadius) then
client.MaximumSimulationRadius = maxRadius
end
client.SimulationRadius = radius
end
else
sethiddenproperty = getfenv().sethiddenproperty
setsimulationradius = getfenv().setsimulationradius
if (not sethiddenproperty) then
warn("Unable to use 'sethiddenproperty'")
end
if (not setsimulationradius) then
warn("Unable to use 'setsimulationradius'")
end
end
end
function Simulation.DisallowSleep()
physics.AllowSleep = false
end
function Simulation.AllowSleep()
physics.AllowSleep = true
end
function Simulation.Extend()
-- Don't multiply math.huge by math.huge -_-
if (not setsimulationradius) then return end
setsimulationradius(9e9, 9e9)
end
function Simulation.ReduceOthers()
if (not sethiddenproperty) then return end
for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
if (player ~= client) then
sethiddenproperty(player, "SimulationRadius", 0)
sethiddenproperty(player, "MaximumSimulationRadius", 0)
end
end
end
function Simulation:Start()
connection = game:GetService("RunService").Stepped:Connect(function()
self.Extend()
self.ReduceOthers()
self.DisallowSleep()
end)
end
function Simulation:Stop()
if (connection) then
connection:Disconnect()
end
for _,player in ipairs(game:GetService("Players"):GetPlayers()) do
sethiddenproperty(player, "SimulationRadius", math.random(8, 22))
sethiddenproperty(player, "MaximumSimulationRadius", math.random(1000, 1300))
end
self.AllowSleep()
end
return Simulation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment