Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My game datastore does not work anymore how to fix it?

Information: This works before now it doesn't No errors

local DSService = game:GetService("DataStoreService"):GetDataStore("Normalslots")

game.Players.PlayerAdded:connect(function(player)
 local uniquekey = "id-"..player.userId
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local Clicks = Instance.new("IntValue")
    Clicks.Name = "Clicks"
    Clicks.Value = 0
    Clicks.Parent = leaderstats

    local Prestiges = Instance.new("IntValue")
    Prestiges.Name = "Prestiges"
    Prestiges.Value = 0
    Prestiges.Parent = leaderstats

    local Coins = Instance.new("IntValue")
    Coins.Name = "Coins"
    Coins.Value = 0
    Coins.Parent = leaderstats

    local Class = Instance.new("IntValue")
    Class.Name = "Class"
    Class.Value = 0
    Class.Parent = leaderstats
    
    local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        Clicks.Value = GetSaved[1]
        Prestiges.Value = GetSaved[2]
        Coins.Value = GetSaved[3]
        Class.Value = GetSaved[4]
    else
        local NumbersForSaving = {Clicks.Value, Prestiges.Value, Coins.Value, Class.Value}
        DSService:SetAsync(uniquekey,NumbersForSaving)
    end
end)

game.Players.PlayerRemoving:connect(function(player)
    local uniquekey = "id-"..player.userId
    local Savetable = {player.leaderstats.Clicks.Value, player.leaderstats.Prestiges.Value, player.leaderstats.Coins.Value, player.leaderstats.Class.Value}
    DSService:SetAsync(uniquekey,Savetable)

end)

1 answer

Well, there's some capitalization errors, just make :Connect instead of :connect. Also, I noticed, why you are saving data when a new player joins. There's no need for it.

-- It will be fine, you don't need to save if data isn't found
local GetSaved = DSService:GetAsync(uniquekey)
    if GetSaved then
        Clicks.Value = GetSaved[1]
        Prestiges.Value = GetSaved[2]
        Coins.Value = GetSaved[3]
        Class.Value = GetSaved[4]
    end

Also, you don't have BindToClose, which triggers when the server shutdowns or server closed by the owner. It is helpful to delay the game shutdown, so that, every player's data will be saved. Here's an example :

game:BindToClose(function()
	local RS = game:GetService('RunService')
	if RS:IsStudio() then return end -- It prevents the function to run while testing in studio
	for _, player in ipairs(game.Players;GetPlayers()) do -- Looping through the player's table
		-- Your code for saving data
	end
end)

Also, if you want to test it in studio, just comment out your PlayerRemoving script and this line also, if RS:IsStudio() then return end.

Lemme know if it helps!

This discussion is preserved as it was. New answers, comments, and votes are closed.