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

Why is my DataStore not saving data?

I'm making a data store, I have needed help with this before but now there's a new problem. It's not saving!

Here is my script.

local datastore = game:GetService("DataStoreService")
local  Exp = datastore:GetDataStore("Exp")
local  Toony = datastore:GetDataStore("Toony")
local  Level = datastore:GetDataStore("Level")
local playersleft = 0


game.Players.PlayerAdded:Connect(function(player)
    playersleft = playersleft + 1
    local leaderstats = Instance.new("Folder",player)
    leaderstats.Name = "leaderstats"
    local exp = Instance.new("IntValue",leaderstats)
    exp.Name = "Exp"
    local maxexp = Instance.new("IntValue",leaderstats)
    maxexp.Name = "MaxExp"
    maxexp.Value = 100
    local level = Instance.new("IntValue",leaderstats)
    level.Name = "Level"
    local toonys = Instance.new("IntValue",leaderstats)
    toonys.Name = "Toonys"	
	game.ReplicatedStorage.Events.LevelOneGive.Event:Connect(function()		
		toonys.Value = toonys.Value + math.random(10,20)
		exp.Value = exp.Value + 10
	end)
	
	if exp.Value <= 100 then
	exp.Value = 0 
	level.Value = level.Value + 1
    end

    local exp_data
    local level_data
    local toony_data


    pcall(function()
        exp_data = Exp:GetAsync(player.UserId.." Exp")
        level_data = Level:GetAsync(player.UserId.." Level")
        toony_data = Toony:GetAsync(player.UserId.."Toonys")	
		print("Getting data!")
    end)

    if toony_data ~= nil and level_data ~= nil and exp_data ~= nil then
        toonys.Value = toony_data
        exp.Value = exp_data
        level.Value = level_data
		print("Data loaded!")
    else
        toonys.Value = 5
        level.Value = 1
        exp.Value = 0
		print("New player!")
    end
end)

game.Players.PlayerRemoving:Connect(function(player)
    pcall(function()
        playersleft = playersleft - 1
        Exp:SetAsync(player.UserId.." Exp",player.leaderstats.Exp.Value)
        Level:SetAsync(player.UserId.." Level",player.leaderstats.Level.Value)
        Toony:SetAsync(player.UserId.." Toonys",player.leaderstats.Toonys.Value)
        print("Saved!")

    end)
end)

1 answer

✓ Accepted answer

Id recommend using a single datastore to save all the datas instead of three. This would also close the worry about limitations by a lot more.

It would look something like this.

local datas = game:GetService("DataStoreService"):GetDataStore("PlayerData");

-- OnAdded
local data = data:GetAsync(player.UserId) or {
	Level = 1, -- Default values :)
	Exp = 0,
	Money = 200
}

--Save
data:SetAsync(player.UserId, {Level = level.Value, Exp = exp.Value, Money = money.Value});

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