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

How do I fix datastore not saving or loading data?

My datastore won't save its data. I am making a playtime leaderboard but it won't save data when rejoined. The output says the error message in warn(errormessage)

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local playtime = Instance.new("IntValue")
	playtime.Name = "Playtime"
	playtime.Parent = leaderstats
	
	local data
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-playtime")
	end)
	
	if success then
		playtime.Value = data
	else
		print("Player Playtime error whrist saving")
		warn(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local success, errormessage = pcall(function()
		myDataStore:SetAsync(player.UserId.."-playtime",player.leaderstats.Playtime.Value)
	end)
	
	if success then
		print("Player Playtime saved")
	else
		print("Player Playtime error whrist saving")
		warn(errormessage)
	end
	
end)

ammount =  1
timedelay = 1
currencyname = "Playtime"

while true do
	wait(timedelay)
	for i,v in pairs(game.Players:GetPlayers()) do
		if v:FindFirstChild("leaderstats") then
			v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + ammount
		end
	end
end

2 comments

0

Use the game:BindToClose method because if you are trying to save in studio the server will close before it can save the data.

0

no it wont even save too even on a client

1 answer

Hello. The problem is that you used :SetAsync() on the PlayerAdded event instead of :GetAsync(). Another problem is that you made a while true do loop, which means the script would get stuck looping in that loop and never get to the PlayerAdded and/or PlayerRemoving. This is easily fixed by use a coroutine which runs code without yielding the rest of the script. Try this:

local DataStoreService = game:GetService("DataStoreService")

local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local playtime = Instance.new("IntValue")
    playtime.Name = "Playtime"
    playtime.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        myDataStore:GetAsync(player.UserId.."-playtime")
    end)

    if success then
        playtime.Value = data
    else
        print("Player Playtime error whrist saving")
        warn(errormessage)
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
        myDataStore:SetAsync(player.UserId.."-playtime",player.leaderstats.Playtime.Value)
    end)

    if success then
        print("Player Playtime saved")
    else
        print("Player Playtime error whrist saving")
        warn(errormessage)
    end

end)

local ammount =  1
local timedelay = 1
local currencyname = "Playtime"

coroutine.wrap(function()
	while true do
	    wait(timedelay)
	    for i,v in pairs(game.Players:GetPlayers()) do
	        if v:FindFirstChild("leaderstats") then
	            v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + ammount
	        end
	    end
	end
end)()

Also, don't forget to put local before your variables as it makes them global and slower. Please upvote and accept this answer if it helps.

1 comment

0

it did remove the error message but it wont save still

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