The date store is not working

this is a local script for quests

local Players = game:GetService("Players")
local moduleSc = require(game.ReplicatedStorage.Abbreviate)
local player = Players.LocalPlayer
local strength = player:WaitForChild("Strength")
local coins = player:WaitForChild("Coins")
local questFrame = script.Parent.Parent.Parent.Parent.Parent.QuestGui.Frame
local time1 = questFrame.DailyQuest.Time
local reward = questFrame.DailyQuest.QuestReward
local percent = questFrame.DailyQuest:WaitForChild("Percent")
local claim = questFrame.DailyQuest:WaitForChild("Claim")
local progressBar = questFrame.DailyQuest:WaitForChild("bar")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QuestDataRemote = ReplicatedStorage:WaitForChild("QuestDataRemote")

local dailyQuests = {
    {description = "Collect 100 strength", target = 100, rewardAmount = 100},
    {description = "Collect 1000 strength", target = 1000, rewardAmount = 250},
    {description = "Collect 100000 strength", target = 100000, rewardAmount = 500},
    {description = "Collect 1000000 strength", target = 1000000, rewardAmount = 700},
    {description = "Collect 10000000 strength", target = 10000000, rewardAmount = 1500},
    {description = "Collect 100000000 strength", target = 100000000, rewardAmount = 2000},
    {description = "Collect 1000000000 strength", target = 1000000000, rewardAmount = 2400},
    {description = "Collect 10000000000 strength", target = 10000000000, rewardAmount = 2700},
    {description = "Collect 100000000000 strength", target = 100000000000, rewardAmount = 3000},
    {description = "Collect 1000000000000 strength", target = 1e12, rewardAmount = 3500},
    {description = "Collect 10000000000000 strength", target = 1e13, rewardAmount = 4500},
    {description = "Collect 100000000000000 strength", target = 1e14, rewardAmount = 5000},
    {description = "Collect 1000000000000000 strength", target = 1e15, rewardAmount = 5500},
    {description = "Collect 10000000000000000 strength", target = 1e16, rewardAmount = 6000},
    {description = "Collect 100000000000000000 strength", target = 1e17, rewardAmount = 7000},
    {description = "Collect 1000000000000000000 strength", target = 1e18, rewardAmount = 8500},
    {description = "Collect 10000000000000000000 strength", target = 1e19, rewardAmount = 9500},
    {description = "Collect 100000000000000000000 strength", target = 1e20, rewardAmount = 10000},
    {description = "Collect 1000000000000000000000 strength", target = 1e21, rewardAmount = 12500},
    {description = "Collect 10000000000000000000000 strength", target = 1e22, rewardAmount = 15000},
    {description = "Collect 100000000000000000000000 strength", target = 1e23, rewardAmount = 17500},
    {description = "Collect 1000000000000000000000000 strength", target = 1e24, rewardAmount = 20000},
    {description = "Collect 10000000000000000000000000 strength", target = 1e25, rewardAmount = 25000},
    {description = "Collect 100000000000000000000000000 strength", target = 1e26, rewardAmount = 30000},
    {description = "Collect 1000000000000000000000000000 strength", target = 1e27, rewardAmount = 35000},
    {description = "Collect 10000000000000000000000000000 strength", target = 1e28, rewardAmount = 40000},
    {description = "Collect 100000000000000000000000000000 strength", target = 1e29, rewardAmount = 45000},
    {description = "Collect 1000000000000000000000000000000 strength", target = 1e30, rewardAmount = 50000},
}

local currentQuestIndex = player:GetAttribute("CurrentQuestIndex") or 1
local questCompleted = false
local maxQuestsCompleted = #dailyQuests

local function getRemainingTime()
    local currentTime = os.time()
    local resetTime = os.time({year=os.date("%Y"), month=os.date("%m"), day=os.date("%d"), hour=23, min=59, sec=59})

    if currentTime >= resetTime then
        resetTime = resetTime + 86400
    end

    return resetTime - currentTime
end

local function formatTime(seconds)
    local hours = math.floor(seconds / 3600)
    seconds = seconds % 3600
    local minutes = math.floor(seconds / 60)
    seconds = seconds % 60
    return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end

local function updateQuest()
    if currentQuestIndex > maxQuestsCompleted then
        percent.Text = "MAX"
        reward.Text = ""
        claim.Visible = false
        progressBar.Size = UDim2.new(1, 0, 1, 0)
    else
        local currentQuest = dailyQuests[currentQuestIndex]
        percent.Text = "Goal Strength: 0 / " .. moduleSc.abbreviate(currentQuest.target)
        reward.Text = "Reward: " .. currentQuest.rewardAmount .. " coins"
        claim.Visible = false
        progressBar.Size = UDim2.new(0, 0, 1, 0)
        progressBar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
    end
end

local function updateProgressBar()
    if currentQuestIndex > maxQuestsCompleted then return end

    local currentQuest = dailyQuests[currentQuestIndex]
    local progress = math.min(strength.Value / currentQuest.target, 1)

    percent.Text = "Strength: " .. moduleSc.abbreviate(strength.Value) .. " / " .. moduleSc.abbreviate(currentQuest.target)
    progressBar.Size = UDim2.new(progress, 0, 1, 0)

    if strength.Value >= currentQuest.target then
        questCompleted = true
        claim.Visible = true
        percent.Text = "Completed! You can claim the reward."
        progressBar.BackgroundColor3 = Color3.fromRGB(255, 0, 0)
    else
        questCompleted = false
        claim.Visible = false
        progressBar.BackgroundColor3 = Color3.fromRGB(0, 255, 0)
    end
end

local function claimReward()
    if questCompleted then
        coins.Value = coins.Value + dailyQuests[currentQuestIndex].rewardAmount
        currentQuestIndex = currentQuestIndex + 1

        -- If the player has completed all quests, start from the first one
        if currentQuestIndex > maxQuestsCompleted then
            currentQuestIndex = 1
        end

        print("Player claimed reward. CurrentQuestIndex is now: " .. currentQuestIndex)
        player:SetAttribute("CurrentQuestIndex", currentQuestIndex) -- Save the quest index
        updateQuest() -- Update the quest
        QuestDataRemote:FireServer("SaveQuestData", currentQuestIndex) -- Send data to the server
    end
end

-- Initialize the quest
updateQuest()

-- Update the timer every second
coroutine.wrap(function()
    while true do
        time1.Text = "Time remaining: " .. formatTime(getRemainingTime())
        wait(1)
    end
end)()

-- Subscribe to changes in player strength
strength.Changed:Connect(function()
    updateProgressBar()
end)

-- Subscribe to the button click
claim.MouseButton1Click:Connect(claimReward)

print("Local script is running. CurrentQuestIndex: " .. currentQuestIndex)

and this is a server script for the date of the quest store. I want the quest to be saved every time the player picks up the reward.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local QuestDataStore = DataStoreService:GetDataStore("PlayerQuestData")  -- Data store for quests

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local QuestDataRemote = ReplicatedStorage:WaitForChild("QuestDataRemote")

-- Function to save quest data
local function saveQuestData(player, currentQuestIndex)
    local userId = player.UserId
    local success, err = pcall(function()
        QuestDataStore:UpdateAsync(tostring(userId), function(oldData)
            return currentQuestIndex
        end)
    end)

    if success then
        print("Quest data successfully saved for player " .. player.Name .. " (QuestIndex: " .. currentQuestIndex .. ")")
    else
        warn("Failed to save quest data for player " .. player.Name .. ": " .. err)
    end
end

-- Function to load quest data
local function loadQuestData(player)
    local userId = player.UserId
    local savedQuestIndex = 1  -- Initial quest value if no data exists

    local success, err = pcall(function()
        savedQuestIndex = QuestDataStore:GetAsync(tostring(userId)) or 1
    end)

    if success then
        print("Quest data loaded for player " .. player.Name .. " (QuestIndex: " .. savedQuestIndex .. ")")
    else
        warn("Failed to load quest data for player " .. player.Name .. ": " .. err)
    end

    -- Set the attribute to store the player's current quest
    player:SetAttribute("CurrentQuestIndex", savedQuestIndex)
end

-- Subscribe to player events
Players.PlayerAdded:Connect(function(player)
    -- Load quest data when the player joins
    loadQuestData(player)

    -- Handle events from the client
    QuestDataRemote.OnServerEvent:Connect(function(player, action, currentQuestIndex)
        if action == "SaveQuestData" then
            print("Received SaveQuestData event from player: " .. player.Name .. " with quest index: " .. currentQuestIndex)
            saveQuestData(player, currentQuestIndex)
        end
    end)
end)

Players.PlayerRemoving:Connect(function(player)
    local currentQuestIndex = player:GetAttribute("CurrentQuestIndex") or 1
    saveQuestData(player, currentQuestIndex)
end)

I fixed it now it is saved And no one even helped

1 Like

I assume no one replied because you didn’t provide enough information.
You say that the data store is not working - is it erroring? Is it not saving the data correctly? Is it not loading? You need to provide more information.

Regardless: Given that you’ve solve it on your own, please reply to your post stating what your solution was and then mark your reply as the solution.

2 Likes

I tested it in roblox studio not roblox and it worked

1 Like

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