Hi, im making this post today becuase when my tycoon spawns parts (a lot of parts at a time) it causes a lot of lag and increases the CPU a lot
local Main = require(game.ReplicatedStorage.Framework)
local Action : RemoteEvent = Main:GetElement("Action")
local RunService = game:GetService("RunService")
local OreController = {}
local ConveyorTable = {}
local ActiveOres = {}
local ActiveOreCount = 0
local DefaultSpeed = 20
local RecheckInterval = 0.4
task.spawn(function()
task.wait(5)
for _, v in pairs(workspace:GetDescendants()) do
if v.Name == "DONOTCHANGE" then
table.insert(ConveyorTable, v)
end
end
end)
local function CountActive()
return ActiveOreCount
end
local function FindConveyor(position)
local rp = RaycastParams.new()
rp.FilterType = Enum.RaycastFilterType.Whitelist
rp.FilterDescendantsInstances = ConveyorTable
local result = workspace:Raycast(position, Vector3.new(0, -10, 0), rp)
if result and result.Instance.Name == "DONOTCHANGE" then
return result.Instance
end
end
local function GetDirAndSpeed(conveyor)
local dir = Vector3.new(1,0,0)
local speed = DefaultSpeed
if conveyor and conveyor:IsA("BasePart") then
dir = conveyor.CFrame.RightVector
local aDir = conveyor:GetAttribute("Direction")
if typeof(aDir) == "Vector3" and aDir.Magnitude > 0 then dir = aDir.Unit end
local v3 = conveyor:FindFirstChild("Direction")
if v3 and v3:IsA("Vector3Value") and v3.Value.Magnitude > 0 then dir = v3.Value.Unit end
local att = conveyor:FindFirstChild("Dir")
if att and att:IsA("Attachment") then dir = att.WorldCFrame.LookVector.Unit end
local aSpeed = conveyor:GetAttribute("Speed")
if typeof(aSpeed) == "number" then speed = aSpeed end
local n = conveyor:FindFirstChild("Speed")
if n and n:IsA("NumberValue") then speed = n.Value end
end
return dir, speed
end
local function GetRandomOre()
local total = 0
for _, data in pairs(Main:GetClass("OreData")) do
total += data.Chance
end
local roll = math.random() * total
local cum = 0
for name, data in pairs(Main:GetClass("OreData")) do
cum += data.Chance
if roll <= cum then
return name
end
end
end
RunService.Heartbeat:Connect(function(dt)
for ore, data in pairs(ActiveOres) do
if ore and ore.Parent and ore.PrimaryPart then
data.Timer = (data.Timer or 0) + dt
if data.Timer >= RecheckInterval then
data.Timer = 0
data.Conveyor = FindConveyor(ore.PrimaryPart.Position)
if data.Conveyor then
local _, spd = GetDirAndSpeed(data.Conveyor)
data.Speed = spd
end
end
if data.Conveyor then
local dir, spd = GetDirAndSpeed(data.Conveyor)
local step = dir * (spd * dt)
ore.PrimaryPart.CFrame = ore.PrimaryPart.CFrame + step
end
else
if ActiveOres[ore] then
ActiveOres[ore] = nil
ActiveOreCount -= 1
end
end
end
end)
function OreController.Init()
Action.OnClientEvent:Connect(function(ActionName, DropperArea, Player, tycoonData)
if ActionName == "MakeOre" then
if CountActive() >= 300 then return end
local oreName = tycoonData.SpecialDrill and ("Refined"..GetRandomOre()) or GetRandomOre()
local Ore = Main:GetElement(oreName):Clone()
Ore.Parent = workspace.ExistingOres
Ore:MoveTo(DropperArea.Position - Vector3.new(0, 2.25, 0))
if Ore:IsA("Model") and not Ore.PrimaryPart then
Ore.PrimaryPart = Ore:FindFirstChildWhichIsA("BasePart")
end
for _, p in ipairs(Ore:GetDescendants()) do
if p:IsA("BasePart") then
p.Massless = true
p.CastShadow = false
p.CollisionGroup = "Ores"
p:SetAttribute("Multiplier", 1)
p:SetAttribute("Size", tycoonData.Size)
p:SetAttribute("Player", Player.Name)
if not tycoonData.SpecialDrill then
p:SetAttribute("IsOre", true)
else
p:SetAttribute("IsRefined", true)
end
end
end
Ore:SetAttribute("Multiplier", 1)
Ore:SetAttribute("Size", tycoonData.Size)
Ore:SetAttribute("Player", Player.Name)
local cv = FindConveyor(Ore.PrimaryPart.Position)
local _, spd = GetDirAndSpeed(cv)
ActiveOres[Ore] = {Conveyor = cv, Speed = spd, Timer = 0}
ActiveOreCount += 1
Ore.AncestryChanged:Connect(function(_, parent)
if not parent and ActiveOres[Ore] then
ActiveOres[Ore] = nil
ActiveOreCount -= 1
end
end)
task.delay(10, function()
if Ore and Ore.Parent then Ore:Destroy() end
end)
end
end)
end
return OreController
This is the ore spawning script.
Thanks!