Last three days I’ve been stuck on this code. The goal for this script is the have the server script tell the client which frames need to be shown in the store for the day.
-
I need help fixing my code to save the previous dated frames that were shown. (or if someone can recommend another solution, ideally I would like to have this display frame idea so we can add frames later on easily.)
-
The issue is the script is not remembering what were the pervious frames shown (Yesterdays frames).
[My though process was that this script can just see what was shown yesterday and make sure not to have repeating frames displayed for the player, which would be back to back days.]
- I’ve learned a lot of this great post here but couldn’t get my version to work.
How to Make Server-Synced Daily Shops
Server Sided Script to handle rotation
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CountdownTimer = ReplicatedStorage:FindFirstChild("DailyTowerSkinCountDownTimer")
local updateEvent = ReplicatedStorage:WaitForChild("UpdateTowerSkinsDisplay")
local DataStoreService = game:GetService("DataStoreService")
local framesDataStore = DataStoreService:GetDataStore("MarketFramesDataStore")
local manualDate = { year = 2023, month = 1, day = 7 } -- Change this date as needed for testing
-- Function to get today's date as a seed or a specified manual date
local function getTodaySeed()
local now = os.time(manualDate)
local date = os.date("!*t", now)
local seed = date.year * 10000 + date.month * 100 + date.day -- YYYYMMDD format
-- Print the date and the seed for debugging
print(string.format("Using Date: %04d-%02d-%02d", date.year, date.month, date.day))
print("Current Seed:", seed)
return seed
end
local function saveFramesState(framesToShow)
local success, errorMessage = pcall(function()
framesDataStore:SetAsync("LastShownFrames", framesToShow)
end)
if not success then
warn("Failed to save frames state: "..errorMessage)
end
end
local function loadFramesState()
local success, framesToShow = pcall(function()
return framesDataStore:GetAsync("LastShownFrames")
end)
if success and framesToShow then
return framesToShow
else
warn("Failed to load frames state.")
return nil
end
end
local Common = {
"ArcherTowerGreen",
"ArcherTowerGreenTest1",
"ArcherTowerGreenTest2",
"ArcherTowerGreenTest3",
"ArcherTowerGreenTest4",
"ArcherTowerGreenTest5",
"ArcherTowerGreenTest6",
"ArcherTowerGreenTest7",
}
local Uncommon = {
"ArcherTowerUncommonTest1",
"ArcherTowerUncommonTest2",
"ArcherTowerUncommonTest3",
"ArcherTowerUncommonTest4",
"ArcherTowerUncommonTest5",
"ArcherTowerUncommonTest6",
}
local Rare = {
"ArcherTowerTiny",
"ArcherTowerPost",
"ArcherTowerPostTest1",
"ArcherTowerPostTest2",
"ArcherTowerPostTest3",
"ArcherTowerPostTest4",
}
local lastShown = {
Common = {},
Uncommon = {},
Rare = {}
}
local function selectRandomFrames(frameList, numToSelect, lastShown)
local seed = getTodaySeed()
local rng = Random.new(seed)
local tempList = {}
for _, frameName in ipairs(frameList) do
if not table.find(lastShown, frameName) then
table.insert(tempList, frameName)
end
end
if #tempList == 0 then tempList = frameList end
local selectedFrames = {}
while #selectedFrames < numToSelect and #tempList > 0 do
local index = rng:NextInteger(1, #tempList)
table.insert(selectedFrames, tempList[index])
table.remove(tempList, index)
end
return selectedFrames
end
local function activateFrames()
-- Select new frames based on the last shown
local selectedCommon = selectRandomFrames(Common, 3, lastShown.Common)
local selectedUncommon = selectRandomFrames(Uncommon, 2, lastShown.Uncommon)
local selectedRare = selectRandomFrames(Rare, 1, lastShown.Rare)
local framesToShow = {
Show = {
Common = selectedCommon,
Uncommon = selectedUncommon,
Rare = selectedRare
}
}
-- Update the global lastShown with the newly shown frames for the next cycle
lastShown = {
Common = selectedCommon,
Uncommon = selectedUncommon,
Rare = selectedRare
}
-- Save the current state to DataStore
saveFramesState(framesToShow)
-- Log the frames being shown
print("Activated new frames.")
for rarity, frames in pairs(framesToShow.Show) do
print(rarity .. " Show:", table.concat(frames, ", "))
end
-- Notify all clients about the new frames
updateEvent:FireAllClients(framesToShow)
end
local function getTimeUntilMidnightPST()
local now = os.time()
local utcOffset = -7 * 3600 -- Adjusted for PST
local pstTime = now + utcOffset
local midnightPST = os.date("!*t", pstTime)
midnightPST.hour = 24; midnightPST.min = 0; midnightPST.sec = 0
local midnightPSTinUTC = os.time(midnightPST) - utcOffset
local secondsUntilMidnight = midnightPSTinUTC - now
if secondsUntilMidnight <= 0 then
secondsUntilMidnight = secondsUntilMidnight + 86400 -- Next day
end
return secondsUntilMidnight
end
local function updateCountdown()
while true do
local secondsUntilMidnight = getTimeUntilMidnightPST()
CountdownTimer.Value = secondsUntilMidnight
if secondsUntilMidnight <= 0 then
activateFrames()
end
wait(1)
end
end
game.Players.PlayerAdded:Connect(function(player)
local framesToShow = loadFramesState() or activateFrames()
updateEvent:FireClient(player, framesToShow)
end)
updateCountdown()
client sided script to handle visual update
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local updateEvent = ReplicatedStorage:WaitForChild("UpdateTowerSkinsDisplay")
-- Ensure the player's GUI is fully loaded
local playerGui = Players.LocalPlayer:WaitForChild("PlayerGui")
local shopGUI = playerGui:WaitForChild("Shop", 10) -- Timeout for safety
assert(shopGUI, "Shop GUI not found")
local framePath = shopGUI:WaitForChild("Frame"):WaitForChild("TowerSkinShop"):WaitForChild("TowerShopScroll"):WaitForChild("DailySkinsFrames")
local function updateFramesVisibility(framesToShow)
print("Received frames to update on client.")
for category, frames in pairs(framesToShow.Show or {}) do
print("Showing frames in category: "..category)
for _, frameName in ipairs(frames) do
local frame = framePath:FindFirstChild(frameName)
if frame then
frame.Visible = true
print("Showing frame: "..frameName)
else
print("Frame not found: "..frameName)
end
end
end
for category, frames in pairs(framesToShow.Hide or {}) do
print("Hiding frames in category: "..category)
for _, frameName in ipairs(frames) do
local frame = framePath:FindFirstChild(frameName)
if frame then
frame.Visible = false
print("Hiding frame: "..frameName)
else
print("Frame not found: "..frameName)
end
end
end
end
updateEvent.OnClientEvent:Connect(updateFramesVisibility)
Thank you