local function DeleteTycoon(player)
print(player.Name.." has left the game!")
for _, Tycoon in pairs(TycoonsHolder:GetChildren()) do
local TycoonOwner = Tycoon:FindFirstChild("TycoonOwner").Value
local CashToSave = Tycoon:FindFirstChild("CashToCollect")
if TycoonOwner then
if TycoonOwner == player then
local StatsFolder = player:FindFirstChild("StatsFolder")
local TycoonSavedCash = StatsFolder:FindFirstChild("TycoonSavedCash")
TycoonSavedCash.Value = CashToSave.Value
local StatsFolder = player:FindFirstChild("StatsFolder")
local TimePlayed = StatsFolder:FindFirstChild("TimePlayed")
local Start = tick()
Tycoon:Destroy()
print("Performance check: [Destroying tycoon]: "..tick()-Start)
local Start2 = tick()
TycoonCloneHolder:FindFirstChild(Tycoon.Name).Parent = TycoonsHolder
print("Performance check: [Parenting tycoon back]: "..tick()-Start2)
print(player.Name.." has left the game. Successfully destroyed their tycoon!")
end
end
end
end
Hello! I’ve currently released my first tycoon and everything is going well except for the major lag that occurs when a player leaves and rejoins the game. (The above code is connected to a PlayerRemoving event in the DataStore script.)
How the tycoons function:
- Each tycoon has its own script that calls 4 functions,
InitializePurchasingPads(), SetDisplayCollectorAmount(), CreatePurchasePads() and InitializeMoneyGiver()
- When the first player joins, all the tycoons run these functions and sets up the tycoons ready to be used for all players.
- When a player joins,
LoadSavedBuildings()
is called and basically loads every building through a for-loop (this is inside the tycoon script). - When a player leaves, the tycoon is destroyed and reparented to the workspace.
Above, I have marked down the speed for how long the Destroy and Reparenting takes for 1 tycoon in the game and the results were:
Performance check: [Destroying tycoon]: 0.14637398719787598
Performance check: [Parenting tycoon back]: 0.6646103858947754
The performance check is in studio with 1 tycoon but it doesn’t seem like it’s adding up as in a real game it lags the entire server for 1-2 seconds. I’m assuming that the actual problem is due to the fact that destroying the tycoon which has hundreds of models that contain 10-20 parts in each model and replicating it to all clients and therefore causing issues. What would be the best way to remove the tycoon without causing major lag? In addition if there is a better way to manage the tycoons in general?
All help is greatly appreciated.
Thank you!