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
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.
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.
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.
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:
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().
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()