Hello scripters.
Today I decided to play around and script something.
I made a pretty simple script that generated grass (All of the grass is mesh parts, collision set to box, uncollideable, etc).
Though, the problem I have is that after some time passed the game starts to lag.
What is the best way to optimalize it but keep the same visual effects?
Here is a screenshot of what it looks like.
Here is the script’s content.
What could be done better?
Note that script activity stays below 1.5%.
local rs = game:GetService("RunService")
local cam = workspace.CurrentCamera
local folder = game:GetService("ReplicatedStorage"):WaitForChild("RubysBeautifulWorld")
local grass = folder:WaitForChild("grass")
local raywhitelist = {workspace:WaitForChild("World")}
local bin = workspace:FindFirstChild("visualbin")
if bin then bin:Destroy() end
--if not bin then
bin = Instance.new("Folder")
bin.Name = "visualbin"
bin.Parent = workspace
--end
local rdvalue = script.renderdistance.Value * 10
local objtable = {}
local objs = 0
local maxobjs = (script.quality.Value * 20) * script.renderdistance.Value
--local multiplier = 50
local time = tick()
function update()
rdvalue = script.renderdistance.Value
maxobjs = (script.quality.Value * 20) * script.renderdistance.Value
print("Settings were updated.")
end
local function round(x)
local up = math.ceil(x)
if x + 0.5 >= up then
return up
else
return math.floor(x)
end
end
--local function roundvector3(x, y, z)
-- return Vector3.new(round(x), round(y), round(z))
--end
local camv3 = function() return cam.CFrame.Position end
local function raydown(pos)
--local ray = Ray.new(pos, CFrame.new(pos, pos - Vector3.new(0,10,0)).LookVector)
return workspace:FindPartOnRayWithWhitelist(Ray.new(pos, CFrame.new(pos, pos - Vector3.new(0,70,0)).LookVector*100), raywhitelist, true)
end
function remove(obj)
for k, v in ipairs(objtable) do
if v == obj then
table.remove(objtable, k)
obj:Destroy()
objs = objs - 1
return true --Success
end
end
return false --Failure
end
local function cleanup()
for k, v in ipairs(objtable) do
if (v.Position - cam.CFrame.Position).magnitude > script.renderdistance.Value * 10 and objs > (maxobjs / 6) * 3 then
remove(v)
end
end
end
local function addgrass(pos, p)
if objs >= maxobjs then return end
local g = grass:Clone()
g.CFrame = CFrame.new(pos)
g.Color = p.Color
--g.Material = p.Material
g.Parent = bin
table.insert(objtable, #objtable, g)
objs = objs + 1
end
function randomgrass()
local hit, pos = raydown(camv3() + Vector3.new(math.random(-rdvalue, rdvalue), 5, math.random(-rdvalue, rdvalue)))
if hit and hit.Material == Enum.Material.Grass then
addgrass(pos, hit)
end
end
script.renderdistance.Changed:Connect(update)
script.quality.Changed:Connect(update)
spawn(function()
while wait(3) do
print("Object count:", objs)
cleanup()
end
end)
while rs.Heartbeat:wait() and workspace.CurrentCamera do
if objs < maxobjs then
--
--for x = 1, round(math.random(script.renderdistance.Value, script.renderdistance.Value * 20)) do
randomgrass()
randomgrass()
randomgrass()
--end
--
end
-- if tick() > time then
-- cleanup()
-- time = tick() + 2
-- end
end