How to save os.time() using DataStore2? (convert a function to a value object)

I’m trying to use DataStore2 to store os.time() as I’m creating a daily reward system. I have this module that converts my players data table into the right values, etc.

PlayerData

return {
	Streak = 0,
	LastLogin = os.time,
}

Convertor module

print(type(v))
			if type(v) == 'string' then
				newtype = Instance.new('StringValue')
				newtype.Name = i
				newtype.Value = v
				newtype.Parent = parent
			elseif type(v) == 'number' then
				newtype = Instance.new((v%1==0 and 'IntValue' or 'NumberValue'))
				newtype.Name = i
				newtype.Value = v
				newtype.Parent = parent
			elseif type(v) == 'boolean' then
				newtype = Instance.new('BoolValue')
				newtype.Name = i
				newtype.Value = v
				newtype.Parent = parent
			else
				print('MAKING OBJECT!!!!!!!!!!!!!!!!!!!!!!!!!!')
				newtype = Instance.new('ObjectValue')
				newtype.Name = i
				newtype.Parent = parent
			end

Basically, since os.time() is a function, it creates an ObjectValue. How can I work around this?

1 Like

When you’re doing os.time, you’re basically refering to the function itself, not to the os.time(), in other words thevalue that it returns after being called by adding the ().
There is a difference between function and function()

Just re-raid what you wrote, and fixed my understanding, you can simply check if it’s a function by doing if type(v) == "function" then, because functions themselves are kind of a some sort of datatype

1 Like

Your previous answer seemed to work, I think. I have to wait 24 hours too see :sweat_smile:

1 Like

os.time() returns a number. You can’t save functions with Datastore and it will error if you try. You just need to write os.time() with the parenthesis so that you actually call the function

2 Likes

Yeah very good point, I guess he gotta come up with some sort of way to replace sending the function itself, like maybe simply check if os.time was a function in the part that was supposed to save the data, then send true for example if it was

os is a built-in lua library so that function is gonna still be there in the next game a player joins. If you are trying to make a daily reward system you should only need to save the value returned by os.time(); saving the function itself is pointless.