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

Im trying to do a save system, but isnt working. Any help?

local DS = game:GetService("DataStoreService"):GetDataStore("DataSystem")


local function save(player)
    
    local folder = player:WaitForChild("Data")
    local data = {}
    for k,v in pairs(folder:GetChildren()) do
    if v.ClassName ~= "Folder" then
        data[v.Name] = v.Value
        end
    end

    for i=1,3 do
        if pcall(DS.SetAsync,DS,player.userId,data) then
            return
        end
    end
 
end

local function load(player)
    local folder = player:WaitForChild("Data")
    local suc,data
    for i=1,3 do
        local s,e = pcall(DS.GetAsync,DS,player.userId)
        if s then suc,data = s,e break end
    end
    if suc and data then
        for k,v in pairs(data) do
            local val = folder:FindFirstChild(k)  
            if val then val.Value = v end
        end
       
    elseif suc then
        --No data
    else
		--Error
        return
    end
end



local Players = game:GetService("Players")
Players.PlayerAdded:connect(function(plr)
    local Stats = script.data:Clone()
    Stats.Parent = plr

    load(plr)
end)
Players.PlayerRemoving:connect(function(plr)
    save(plr)
end)

-- Auto save
while wait(30) do
    for k,v in pairs(Players:GetPlayers()) do
        coroutine.wrap(save)(v)
    end
end

I dont even know what i should try anymore, help please

1 answer

I don't really know how to answer your question.. But you can try this script out and see if it'll work for you.

local ds = game:GetService("DataStoreService"):GetDataStore("stats")
local stats={"InsertStatNameHere","InsertStatNameHere"} --Makes different stats
game.Players.PlayerAdded:connect(function(plyr)
	local a=Instance.new("NumberValue")
	a.Parent=plyr
	a.Name="bin"
	for i=1,#stats do
		local stat=Instance.new("NumberValue")
		stat.Parent=a
		stat.Value=0
		stat.Name=stats[i]
	end
	local child=plyr.bin:GetChildren()
	for i=1, #child do
		child[i].Value=ds:GetAsync(plyr.userId..child[i].Name)
	end
end)

game.Players.PlayerRemoving:connect(function(plyr)
	local child=plyr.bin:GetChildren()
	for i=1, #child do
		child[i].Value=ds:SetAsync(plyr.userId..child[i].Name,child[i].Value)
	end
end)

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