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

Datastore script either doesnt save or doesnt load, dont know whats wrong with it?

Im having issues with the following datastore script. There are no errors, and when i leave the game/stop playing, it prints "Data saved" and when i start play testing again, it prints "Data Loaded", but it doesnt save or load. I dont know what is wrong and i dont know if its the save functions or load functions fault. Here is the code:

local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")

local function Load(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Nights = Instance.new("IntValue")
	Nights.Name = "Nights"
	Nights.Parent = leaderstats
	
	print("Leaderstats created")
	
	local data
	local success, errormessage = pcall(function()
		data = datastorage:GetAsync(player.UserId)
	end)
	print("Data load happened")
	
	if success then
		print("Data loaded")
	else
		warn("Error happened while loading data: "..errormessage)
	end
end

local function Save(player)
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
		local success, errormessage = pcall(function()
			datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)		
		end)	
		
		if success then
			print("Data saved")
		else
			warn("Error happened while saving data: "..errormessage)
		end		
	end
end

game.Players.PlayerAdded:Connect(Load)
game:BindToClose(Save)

1 answer

✓ Accepted answer

When you're loading the data you're not actually using it, you're just checking if its there....

local datastoreservice = game:GetService("DataStoreService")
local datastorage = datastoreservice:GetDataStore("datastorage")
print("Variables loaded")

local currentSession = {}

local function Load(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

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

    print("Leaderstats created")

    local data
    local success, errormessage = pcall(function()
	return data = datastorage:GetAsync(player.UserId) --added "return"
    end)
    print("Data load happened")

    if success then --cool, its there, so what?
        print("Data loaded") 
---Here
	if data then --if theres data, act on it
		player.leaderstat.Nights.Value = data
	else
		--setup some default data (for first time-players) etc.
	end
--To here
    else
        warn("Error happened while loading data: "..errormessage)
    end
end

local function Save(player)
    for i, player in pairs(game:GetService("Players"):GetPlayers()) do
        local success, errormessage = pcall(function()
            datastorage:SetAsync(player.UserId, player.leaderstats.Nights.Value)        
        end)    

        if success then
            print("Data saved")
        else
            warn("Error happened while saving data: "..errormessage)
        end     
    end
end

game.Players.PlayerAdded:Connect(Load)
game:BindToClose(Save)

I think this should help? if its still not working let me know and I'll have another look... I'm pretty new to this too so I've probs missed something haha This guide is pretty good!

1 comment

0

sorry for the late response, the script you gave me didnt work as it had a few typos and errors, however i fixed those errors and it works, so ill accept this as an answer. Thank you!

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