So what if I wanted to spawn 10 parts SPECIFICALLY every second? That would be 0.1 seconds for every spawn, so just task.wait(0.1) right? No. What if the framerate is low enough so that it becomes more than every 0.1 seconds? Well I don’t know. I need it to compensate for the time lost, so if there is a 1 second stutter, then spawn 10 parts, but I don’t know how to do this. I’ve been thinking of using tick() or time(), but I don’t know how to implement that.
Okay I can try to point you in the right direction
In the Service: RunService, the method(s) Stepped, RenderStepped, Heartbeat etc. Return a DeltaTime Value. Use this value when spawning your part to consistently match framerate time and how many times you want to spawn a part. Think about this, I might be able to think something up for you in a bit.
I should add that “DeltaTime” Is the amount of time between the current frame, and the last frame.
Yeah I was thinking about that too, something like
math.round(1/dt)
Accumulate deltaTime in a timer (timer += deltaTime), then perform the action and reset the timer when timer >= interval (where interval = 1 / 10).
So essentially, make a interval variable something like 0.1 for 10 times a second.
Set a timer Variable to 0
add timer to delta time each step
if timer >= interval then
-- do something
timer -= interval
-- reset timer, continue renderstepped
(you can thank the internet for this, not me my brain is fried rn lmao)
Unfortunately this doesn’t work because if I limited my fps to 5, it is much slower than uncapped.
You can do this using most of the RunService events (RenderStepped, Stepped, and Heartbeat mainly)
Something like the below could probably work:
local elaspedTime = 0 -- The total elapsed time since last spawn. This will accumulate extra time on its own
-- I used Heartbeat here (because it works server side), however the other functions do work
game:GetService("RunService").Heartbeat:Connect(function(delta)
elapsedTime += delta -- Add the current time between frames to the elapsed time
-- If the elapsedTime is more than our wanted interval
if (elapsedTime > [INTERVAL]) do -- [INTERVAL] is whatever interval you want
-- For every amount of elapsedTime over the interval, do stuff
while (elapsedTime > [INTERVAL]) do
elapsedTime -= [INTERVAL] -- Subtract 1 interval's worth of time from total elapsed time
-- DO PART SPAWNING STUFF
end
-- Now, why do I use a while loop here? Because you want to catch up from any excess time elapsed
-- This makes up for lost time by spawning a corresponding amount of parts for every interval period the elapsedTime is over
end
end)
edit: originally used .Stepped in my example which gives out 2 variables instead of just delta. Changed to be correct
This code, as well as @ilostmyaccount678 works perfectly, thank you all.
local rs = game:GetService("RunService")
local timer = 0
local interval = 0.001
game:GetService("RunService").Heartbeat:Connect(function(dt)
timer += dt
if timer >= interval then
for i = 0,timer-interval,interval do
--(code here)
end
timer = 0
end
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.