Hello scripters and developers, I made a script that simulates leaves movement, but the script usage(%) and the rate/s are kinda high, i need a way to fix that
and overall the script is not well optimized and makes the game not run as smoothly as i want
if any of you can give me a fixed code of it it would help me very much
here is the script:
local RunService = game:GetService("RunService")
local function updatePosition(leafPart)
local pos = leafPart.Position
local tall = leafPart.Size.Y / 2
local T = -99999
local counter = 0
local updateFrequency = 10 -- Update every 10 heartbeats
return function()
counter = counter + 1
if counter >= updateFrequency then
counter = 0
local x = pos.X + (math.sin(T + (pos.X / 5)) * math.sin(T / 9)) / 3
local z = pos.Z + (math.sin(T + (pos.Z / 6)) * math.sin(T / 12)) / 4
leafPart.CFrame = CFrame.new(x, pos.Y, z) * CFrame.Angles((z - pos.Z) / tall, 0, (x - pos.X) / -tall)
T = T + 0.12
end
end
end
local function connectLeaves()
local leaves = workspace:GetDescendants()
for _, leafPart in ipairs(leaves) do
if leafPart:IsA("BasePart") and leafPart.Name == "Leaves" then
local updateFunc = updatePosition(leafPart)
RunService.Heartbeat:Connect(updateFunc)
end
end
end
connectLeaves() -- Initially connect leaves
As you can see in the image down below, you can see that my script (the one in the middle) has an high activity and utilization rate: