Is it just me or is os.time() kinda buggy now?

so around a month ago i asked the devforum on some help with orbital mechanics that based on os.time() so that it syncs between all servers but i dont know why that it got buggy now even though i havent touched the orbital code for a month. the problem is that when i made the script,the planet moved smoothly,but now ever one second the planet gets tp and here is a video on it

instead of teleporting every second it should be moving smoothly and here is the planet script i made in case you wanna test and see for yourself
The Earth Code

local dist = 170 --how far the planet is from the star in studs

local spd = math.rad(360)/(60*60) --where 10 is how many seconds per orbit

local xangle, yangle = 0, 10 --how the planet is tilted in its orbit x/y axis

local orbitparent = script.Parent.Parent.Sun

local lastTimeLogged, count = os.time(), 0

local spin = math.rad(360)/32

game:GetService("RunService").Stepped:Connect(function()

if os.time() > lastTimeLogged then --if its been 1 second, restart count at 0

count = 0

lastTimeLogged = os.time()

end

count += 1

script.Parent.CFrame = orbitparent.CFrame*CFrame.fromEulerAnglesXYZ(xangle,((lastTimeLogged%1000)+(count/60))*spd,yangle)*CFrame.new(0,0,dist)*CFrame.Angles(0, ((lastTimeLogged%1000)+(count/60))*spin,0)

--lastTimeLogged%1000 <- gets us the last 4 integers of os.time(); gives us 1/10000 of accuracy between all servers planetary positions

--for greater accuracy especially at very long orbits, you could increase this to 5+

--count/60 <- this tells us how many 1/60 of a second have passed since the last second; in my tests this was extremely reliable

end)

Please test it and see if it bugs for you.Thanks

use Heartbeat

  • Manage physical states carefully

Stepped happens before physics while Heartbeat happens after physics. Therefore, gameplay logic that affects the physics state should be done in Stepped , such as setting the Velocity of parts. In contrast, gameplay logic that relies on or reacts to the physics state should be handled in Heartbeat , such as reading the Position of parts to detect when they enter defined zones.

1 Like

well i added heartbeat instead of stepped and it still occured and even while loop also makes the problem too

1 Like

This does not really work. Basically, what happens is you incorrectly interpolate between the seconds with the count variable.

One way to fix this is to take os.time and add to it. This slightly deviates from reality over time, but this deviation is so small that it is practically impossible to notice.

local runService = game:GetService('RunService')

local t = os.time()

runService.Heartbeat:Connect(function(step)
   t += step
   print(t)
end)

local function updatePlanet(t)
   -- Now you can use t as a parameter
end

well can you fix my script cuz i didnt really understand what you said lmao sorry im bad at scripting.Thanks

local runService = game:GetService("RunService")

local dist = 170 --how far the planet is from the star in studs
local spd = math.rad(360)/(60*60) --where 10 is how many seconds per orbit
local xangle, yangle = 0, 10 --how the planet is tilted in its orbit x/y axis
local orbitparent = script.Parent.Parent.Sun
local lastTimeLogged, count = os.time(), 0
local spin = math.rad(360)/32

local function update(t)
	script.Parent.CFrame = orbitparent.CFrame
		* CFrame.fromEulerAnglesXYZ(
			xangle,
			t * spd,
			yangle
		) * CFrame.new(0, 0, dist)
		* CFrame.Angles(0, t * spin, 0)
end

local t = os.time()
runService.Heartbeat:Connect(function(step)
	t += step
	
	update(t)
end
1 Like