Os.date() in ServerScript

Is there any possible way I can get a player’s local time when they join a game via ServerScript? This is what I’ve tried doing; however, it only returns Roblox’s server time.

game.Players.PlayerAdded:Connect(function(player)

local dt = os.date("*t", os.time())
print((tostring(dt.hour) .. "." .. tostring(dt.min) .. tostring(dt.sec)))

end)

Am I going to have to pass this information through a RemoteEvent with a LocalScript? That’s a last-resort option I want right now.

You need to subtract the difference between the two times by using a client script to send the player’s local time.

3 Likes

I don’t think you need to use a RemoteEvent, technically ModuleScripts are LocalScripts and they can use game.Players.LocalPlayer, maybe you can try that?

-- Server Script (game.ServerScriptService)

game.Players.PlayerAdded:Connect(function(Plr)
	local dt = require(Plr.PlayerGui:WaitForChild("ModuleScript")).getTime("*t",os.time())
	print((tostring(dt.hour) .. "." .. tostring(dt.min) .. tostring(dt.sec)))
end)
-- Module Script (game.StarterGui)

local module = {}

function module.getTime(f,t)
	return os.date(f,t)
end

return module

Module scripts can be any script based on which script you required from it, therefore your solution does not work.

So the only solution here would be to use remote events.

1 Like

Oh, but how did it get the correct time for me when I tested it?

(Also my time is 8PM, it printed 20)

And how are you sure that it is actually your local time?

When you run os.date() in a serverscript, it will return the time based on where the roblox server is located irl. Testing it in roblox studio would not work because it is only your computer thats hosting the “server”, therefore it would return your local time. However if you were to try this in roblox instead, then it would return a different time based on where the roblox server is from.

In this case you ran it inside of roblox studio, which returned 8pm, but if you were to publish the game and play it in roblox, then it would return a different time. You see where I’m going with this?

1 Like

Oh ok I get it now. Yea remote events probably aren’t that bad though.

1 Like

Is this information going to be passed to a LocalScript, anyway? (Or used for UI, etc…)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.