#so how can i fix this script its not saving the Values

5 messages · Page 1 of 1 (latest)

wet oriole
#
local DataStore = game:GetService('DataStoreService'):GetDataStore('LBozo')
local DataHandler = {}
DataHandler.__index = DataHandler

function DataHandler.new(player)
    return setmetatable({Player = player},DataHandler)
end

function DataHandler:SetData()
    local Player = self.Player
    local ls = Instance.new('Folder',Player)
    ls.Name = 'leaderstats'
    local CostFolder = Instance.new('Folder',Player)
    CostFolder.Name = 'CostFolder'
    local lsvalues = {
        {key = 'Clicks',Value = 0}, -- name the Values
    }
    local success, result = pcall(function()
        return DataStore:GetAsync(Player.UserId)
    end)
    local DoSave = success and result or nil

    for i,v in pairs(lsvalues) do
        local MainValues = Instance.new('NumberValue',ls)
        MainValues.Name = v.key
        MainValues.Value = v.Value
        if DoSave then
            MainValues.Value = DoSave[MainValues.Name]
        end
    end
    local Cost = {
        {key = 'Cost',Value = 100},
    }
    for i,v in pairs(Cost) do
        local CostValue = Instance.new('NumberValue',CostFolder)
        CostValue.Name = v.key
        CostValue.Value = v.Value
        if DoSave then
            CostValue.Value = DoSave[CostValue.Name]
        end
    end
end

function DataHandler:UpdateData()
    local Player = self.Player
    local ls = Player:FindFirstChild('leaderstats')
    local Update = {}
    if ls then
        for i,v in pairs(ls:GetChildren()) do
            Update[v.Name] = v.Value
        end
    end
    local CostFolder = Player:FindFirstChild('CostFolder')
    if CostFolder then
        for i,v in pairs(CostFolder:GetChildren()) do
            Update[v.Name] = v.Value
        end
    end
    -- do the same for the Other Folders
    return Update
end

function DataHandler:SaveData()
    local Player = self.Player
    local DoUpdateData = DataHandler:UpdateData()
    DataStore:SetAsync(Player.UserId, DoUpdateData)
end

return DataHandler```
wet oriole
#

@tiny knot

#

So how do I fix it

tiny knot
#

Based on the code you provided, it seems like you are not actually calling the SaveData function anywhere. Without calling this function, the data will not be saved to the DataStore.

You can call SaveData function at regular intervals using a Task object from the TaskScheduler module. For example:

local DataHandler = require(script.DataHandler)

local player = game.Players.LocalPlayer
local dataHandler = DataHandler.new(player)

-- Save data every 5 seconds
game:GetService("TaskScheduler"):CreateTask(function()
    dataHandler:SaveData()
end, 5)

This will create a new DataHandler instance for the local player and save the data every 5 seconds using a Task. You can adjust the time interval as needed.

Additionally, make sure that you are calling the SaveData function after you update any values. For example, if you want to update the "Clicks" value:

local ls = Player.leaderstats
if ls then
    local clicksValue = ls.Clicks
    clicksValue.Value = clicksValue.Value + 1
    dataHandler:SaveData()
end

in this example, the "Clicks" value is incremented by 1 and then the SaveData function is called to save the updated value to the DataStore.