#How to reset Datastore2 data?

10 messages · Page 1 of 1 (latest)

mild narwhal
#
--services
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local TweenService = game:GetService("TweenService")
--folders
local Remotes = ReplicatedStorage:WaitForChild('Remotes')
local Modules = ReplicatedStorage:WaitForChild('Modules')
local Values = ReplicatedStorage:WaitForChild("Values")
--mods
local functions = require(Modules.functions)
local config = require(Modules.Config)
local ZonePlus = require(Modules.Zone)
local DataStore2 = require(Modules.Datastore2)
local DataEditor = require(Modules.DataEditor)
local PlayerModule = require(Modules.Player)
local uniqueDataKey = "MAINUNIQUEKEY"

game.Players.PlayerAdded:Connect(function(player: Player) 
    
    local inQueue = Instance.new("BoolValue", player)
    inQueue.Name = "inQueue"
    inQueue.Value = true
        
    DataStore2:Combine(uniqueDataKey, "PlayerData505050")
    
    local Values = Instance.new("Folder", player)
    Values.Name = "Values"    
    
    local leaderstats = Instance.new("Folder", player)
    leaderstats.Name = "leaderstats"
    
    local EmotesData = DataStore2("Emotes", player)
    
    
    for valueName, valueData in config.Values do
        
        local storedData = DataStore2(valueName, player)
        local value:NumberValue = Instance.new(valueData.InstanceType)
        value.Value = storedData:Get(valueData.DefaultValue)
        value.Name= valueName
        value.Parent = Values
        
        local leaderstatVal 
        if valueData.showLeaderstats then
            leaderstatVal= value:Clone()
            leaderstatVal.Parent = leaderstats
        end
        
        storedData:OnUpdate(function(newValue)
            value.Value = newValue
            if leaderstatVal then leaderstatVal.Value = newValue end
        end)
        
    end
    
    PlayerModule(player)
    
end)

I've tried changing the main key, combine scope key but nothing seems to reset the old data

little plaza
#

It looks like you're using DataStore2 to handle player data persistence, and you're facing an issue where the old data isn't being reset when you change the main key or combine scope key. This might be happening because DataStore2 caches data from previous sessions and continues to load it even if you modify the keys or scope.

Here are a few things you can try to ensure that the data is correctly reset or cleared:

lavish hemlockBOT
#

studio** You are now Level 2! **studio

little plaza
#

You can explicitly clear the old data stored for the player by calling DataStore2's Set function with a nil or a default value before saving the new data. This will override any previously stored values.

For example, you can clear the existing data in your PlayerAdded function:

lua

game.Players.PlayerAdded:Connect(function(player: Player)
-- Clear any old data before applying the new data
local storedData = DataStore2("PlayerData505050", player)
storedData:Set(nil) -- This will clear any old data saved under this key

-- Now you can set up the new data as usual
local inQueue = Instance.new("BoolValue", player)
inQueue.Name = "inQueue"
inQueue.Value = true

DataStore2:Combine(uniqueDataKey, "PlayerData505050")

local Values = Instance.new("Folder", player)
Values.Name = "Values"    

local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local EmotesData = DataStore2("Emotes", player)

for valueName, valueData in config.Values do
    
    local storedData = DataStore2(valueName, player)
    local value = Instance.new(valueData.InstanceType)
    value.Value = storedData:Get(valueData.DefaultValue)
    value.Name = valueName
    value.Parent = Values
    
    local leaderstatVal 
    if valueData.showLeaderstats then
        leaderstatVal = value:Clone()
        leaderstatVal.Parent = leaderstats
    end
    
    storedData:OnUpdate(function(newValue)
        value.Value = newValue
        if leaderstatVal then leaderstatVal.Value = newValue end
    end)
    
end

PlayerModule(player)

end)

#

You can explicitly clear the old data stored for the player by calling DataStore2's Set function with a nil or a default value before saving the new data. This will override any previously stored values.

For example, you can clear the existing data in your PlayerAdded function:

lua

game.Players.PlayerAdded:Connect(function(player: Player)
-- Clear any old data before applying the new data
local storedData = DataStore2("PlayerData505050", player)
storedData:Set(nil) -- This will clear any old data saved under this key

-- Now you can set up the new data as usual
local inQueue = Instance.new("BoolValue", player)
inQueue.Name = "inQueue"
inQueue.Value = true

DataStore2:Combine(uniqueDataKey, "PlayerData505050")

local Values = Instance.new("Folder", player)
Values.Name = "Values"    

local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local EmotesData = DataStore2("Emotes", player)

for valueName, valueData in config.Values do
    
    local storedData = DataStore2(valueName, player)
    local value = Instance.new(valueData.InstanceType)
    value.Value = storedData:Get(valueData.DefaultValue)
    value.Name = valueName
    value.Parent = Values
    
    local leaderstatVal 
    if valueData.showLeaderstats then
        leaderstatVal = value:Clone()
        leaderstatVal.Parent = leaderstats
    end
    
    storedData:OnUpdate(function(newValue)
        value.Value = newValue
        if leaderstatVal then leaderstatVal.Value = newValue end
    end)
    
end

PlayerModule(player)

end)

#

DataStore2 may cache values in the local storage to optimize loading times. If caching is enabled, the data may not be reloaded correctly when the key is changed. You can try disabling caching for testing purposes by using the ClearCache method.

Here’s how you can do that:

lua

DataStore2:ClearCache(player) -- Clears any cached data for the player
Add this before loading any new data to ensure that the player’s cached data is removed, and the data store fetches fresh values from the datastore.

#

Make sure that your Combine function is set up correctly and that you're not accidentally overriding the keys in a way that conflicts with your data. In your case, you're combining uniqueDataKey with "PlayerData505050". Try testing the Combine function separately to ensure that it's working as expected:

lua

DataStore2:Combine(uniqueDataKey, "PlayerData505050")
If you're changing this scope or key during development, make sure you're using distinct keys so that old data isn't accidentally loaded from an unexpected source.

#

You can print out the current value for PlayerData505050 and other stored keys to see if they are being retrieved and overwritten correctly. This will help in identifying if there are any unexpected values that might be causing issues.

lua

local storedData = DataStore2("PlayerData505050", player)
print("Current Data: ", storedData:Get())
By printing the values, you can confirm whether the old data is still being loaded despite changes in your key or scope.

#

Use storedData:Set(nil) to explicitly clear old data before applying new data.
Use storedData:Set(valueData.DefaultValue) to reset data to default values.
Try calling DataStore2:ClearCache(player) to clear any cached data for the player.
Ensure that Combine is properly configured and you're using distinct keys when testing.
Debug and print out stored data to verify if old data is being loaded.
Try these steps and see if the data resets as expected! Let me know if you need more assistance

mild narwhal