OS Time Help and Setup

Hi, I want to make it so that the function below will choose 5 random skins after 24 hours (starting at 12 am). The thing is I don’t know where to start or how to do this. Help will be appreciated!

function SelectSkins()
	local allSkins = game.ReplicatedStorage.AllSkins:GetChildren()
	local fiveRandomSkins = {}
	
	while #fiveRandomSkins < 5 do -- grab random skins until there are 5 chosen
		local newSkin = allSkins[math.random(#allSkins)] -- get a random skin
		local duplicate = false
		for index, skin in pairs(fiveRandomSkins) do -- check for duplicated skin
			if newSkin == skin then
				duplicate = true
			end
		end
		if not duplicate then
			table.insert(fiveRandomSkins, newSkin) -- add skin to list
		end
	end
	
	return fiveRandomSkins
end
1 Like
wait(86400 - os.time()%86400)
SelectSkins()

There’s 86400 seconds in a day, so os.time()%86400 will return the amount of seconds which have passed since the last midnight (time 00:00), so 86400 - seconds since last midnight should be the amount of time until next midnight.

You want to yield for 24 hours??
What’s the point in calling wait() then?

Likely wouldn’t end up waiting 24 hours since I’m guessing he doesn’t have constant 24 hour servers.

Sure you could say that “you shouldn’t put yields in your scripts like that,” but the other option would likely be a while wait() so both of these methods will require a large amount of time yielding.

How would i setup constant 24 hour servers?

when I said “constant 24 hour servers” I meant that your servers were running at all times which would mean there are 100% of the time players in your game.

I’m pretty sure you would need to store the last time they logged in before checking that.

That will be the case in the future. What about now, though? there aren’t any because it’s still in the development phase.

Then the method I gave you will still work.

I’m pretty sure you would need to store the last time they logged in before checking that.

He didn’t say 24 hours since the person joined the server, he said every 24 hours overall

Where should I put that wait then?

If the only thing in your script is the function you provided above, then you can just insert what I gave you after the function. If you have other things in this script, insert this after the function:

coroutine.wrap(function()
wait(86400 - os.time()%86400)
SelectSkins())()

Well, there’s more. I didn’t think I needed to provide it. I Don’t know if this changes anyting:


for index, skin in pairs(chosenSkins) do -- loop through all chosen skins
	skin:Clone().Parent = game.ReplicatedStorage.Skins -- add skin to `Skins`
end


game.ReplicatedStorage.SendData.OnClientEvent:Connect(function()
	addSkins(skinsData)
end)

Looks to me like that function as part of your skin selection process. Just insert the entire function of skin selection into the coroutine that I gave you, after the wait().

Like this?

coroutine.wrap(function()
	wait(86400 - os.time()%86400)
	function SelectSkins()
		local allSkins = game.ReplicatedStorage.AllSkins:GetChildren()
		local fiveRandomSkins = {}
		
		while #fiveRandomSkins < 5 do -- grab random skins until there are 5 chosen
			local newSkin = allSkins[math.random(#allSkins)] -- get a random skin
			local duplicate = false
			for index, skin in pairs(fiveRandomSkins) do -- check for duplicated skin
				if newSkin == skin then
					duplicate = true
				end
			end
			if not duplicate then
				table.insert(fiveRandomSkins, newSkin) -- add skin to list
			end
		end
		
		return fiveRandomSkins
	end
end)
SelectSkins()()

you don’t have to define your function in the coroutine but I mean you can if you want, it’ll still work
coroutine.wrap(function()
wait(86400 - os.time()%86400)
SelectSkins())()

I’m confused. This is what youre telling me to do:



coroutine.wrap(function()
	wait(86400 - os.time()%86400)
end)

function SelectSkins()
		local allSkins = game.ReplicatedStorage.AllSkins:GetChildren()
		local fiveRandomSkins = {}
		
		while #fiveRandomSkins < 5 do -- grab random skins until there are 5 chosen
			local newSkin = allSkins[math.random(#allSkins)] -- get a random skin
			local duplicate = false
			for index, skin in pairs(fiveRandomSkins) do -- check for duplicated skin
				if newSkin == skin then
					duplicate = true
				end
			end
			if not duplicate then
				table.insert(fiveRandomSkins, newSkin) -- add skin to list
			end
		end
		
		return fiveRandomSkins
	end

SelectSkins()

Right, my bad. I didn’t realize.

This doesn’t have a loop so will it repeat after another 24 hours?

But it still won’t work without storing the time. If the server is empty then it won’t keep counting.

You could just change the wait() to while wait() do, but yes you’re right it won’t loop as it is