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

DataStore either not saving or not loading properly?

it prints that data is saved, loaded etc but when it comes to actually loading the data it doesn't seem to function properly. no error is given

local dataStore = game:GetService("DataStoreService"):GetDataStore("Test3")

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

	
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = player
	
	local kills = Instance.new("IntValue")
	kills.Name = "Eliminations"
	kills.Value = 0
	kills.Parent = stats
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Value = 0
	deaths.Parent = stats
	
	local careerKills = Instance.new("IntValue")
	careerKills.Name = "Career Eliminations"
	careerKills.Parent = stats
	
	local careerDeaths = Instance.new("IntValue")
	careerDeaths.Name = "Career Deaths"
	careerDeaths.Parent = stats
	
	local careerWins = Instance.new("IntValue")
	careerWins.Name = "Career Wins"
	careerWins.Parent = stats
	
	local careerLosses = Instance.new("IntValue")
	careerLosses.Name = "Career Losses"
	careerLosses.Parent = stats
	
	local credits = Instance.new("IntValue")
	credits.Name = "Credits"
	credits.Parent = stats
	
	local ID = "SAVE-"..player.UserId
	local saves = {careerKills.Value,careerDeaths.Value,careerWins.Value,careerLosses.Value,credits.Value}
	local savedData = nil
	
	pcall(function()
		savedData = dataStore:GetAsync(ID,saves)
	end)
	
	if savedData == nil then
		careerWins.Value = 0
		careerLosses.Value = 0
		careerKills.Value = 0
		careerDeaths.Value = 0
		credits.Value = 500
		print("New Player")
	else
		careerKills.Value = savedData[1]
		careerDeaths.Value = savedData[2]
		careerWins.Value = savedData[3]
		careerLosses.Value = savedData[4]
		credits.Value = savedData[5]
		print("Data Loaded")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local ID = "SAVE-"..player.UserId
	local careerKills =  player.leaderstats["Career Eliminations"]
	local careerDeaths =  player.leaderstats["Career Deaths"]
	local careerWins =  player.leaderstats["Career Wins"]
	local careerLosses =  player.leaderstats["Career Losses"]
	local credits =  player.leaderstats["Credits"]
	local saves = {careerKills.Value,careerDeaths.Value,careerWins.Value,careerLosses.Value,credits.Value}

		pcall(function()
		dataStore:SetAsync(ID,saves)
		print("Saved")
		end)
	end)

game:BindToClose(function()
	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick("Shutting Down")
		end
	end
	
	wait(5)
	end)


local function AutoSave()
	while true do
		local ID = "SAVES-"..Player.UserId
		local saves = {careerKills.Value,careerDeaths.Value,careerWins.Value,careerLoses.Value,credits.Value}
		
		dataStore:SetAsync(ID,saves)
		print("Auto Save")
		wait(60)
	end
end


1 answer

✓ Accepted answer

GetAsync() only takes one parameter.

You also didnt define player in the autosave loop so that would error your code, stopping it from running.

I rewrote the code a bit, adding a function to Save the player's data, load the player's data, and create the leaderstat items


#Troubleshooting:

It worked when I tested it BUT if you have any issues with saving, try these troubleshooting:

  1. Go to Studio -> Home tab -> Game settings (it's a blue gear) -> Options -> and turn on all three options.

  2. Sometimes studio is wonky not saving datastores, so try playing in game and see what happens.

  3. Another thing to look out for, make sure you're changing the values inside the objects on the server If you change them in the properties in play mode or in a local script, they won't change. only if you change them in a server script or [click this button to go into server mode (https://imgur.com/a/pSjaUYT)

If neither of those work, we can take a look at your game thru team create and see why it isnt working.


#Code:

local dataStore = game:GetService("DataStoreService"):GetDataStore("Test4")

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

    local kills = Instance.new("IntValue")
    kills.Name = "Eliminations"
    kills.Value = 0
    kills.Parent = leaderstatsFolder

    local deaths = Instance.new("IntValue")
    deaths.Name = "Deaths"
    deaths.Value = 0
    deaths.Parent = leaderstatsFolder

    local careerKills = Instance.new("IntValue")
    careerKills.Name = "Career Eliminations"
    careerKills.Parent = leaderstatsFolder

    local careerDeaths = Instance.new("IntValue")
    careerDeaths.Name = "Career Deaths"
    careerDeaths.Parent = leaderstatsFolder

    local careerWins = Instance.new("IntValue")
    careerWins.Name = "Career Wins"
    careerWins.Parent = leaderstatsFolder

    local careerLosses = Instance.new("IntValue")
    careerLosses.Name = "Career Losses"
    careerLosses.Parent = leaderstatsFolder

    local credits = Instance.new("IntValue")
    credits.Name = "Credits"
    credits.Parent = leaderstatsFolder

	return careerKills, careerDeaths, careerWins, careerLosses, credits
end

function saveData(player)
    local careerKills =  player.leaderstats["Career Eliminations"]
    local careerDeaths =  player.leaderstats["Career Deaths"]
    local careerWins =  player.leaderstats["Career Wins"]
    local careerLosses =  player.leaderstats["Career Losses"]
    local credits =  player.leaderstats["Credits"]

	print("values to save", careerKills.Value,careerDeaths.Value,careerWins.Value,careerLosses.Value,credits.Value)

    local saves = {careerKills.Value,careerDeaths.Value,careerWins.Value,careerLosses.Value,credits.Value}

	local success, err = pcall(function()
        dataStore:SetAsync("SAVE-"..player.UserId,saves)
        print("Saved data for "..player.Name)
	end)
	
	if not success then error(err) end	
end

function getData(player)
	local dataFromDatastore 
	
	local success, err = pcall(function()
        dataFromDatastore = dataStore:GetAsync("SAVE-"..player.UserId)
    end)
	
	if not success then error(err) end	
	return dataFromDatastore
end

game.Players.PlayerAdded:Connect(function(player)
	local careerKills, careerDeaths, careerWins, careerLosses, credits = createValueObjects(player)
    local dataFromDatastore = getData(player)


    if not dataFromDatastore then
        careerWins.Value = 0
        careerLosses.Value = 0
        careerKills.Value = 0
        careerDeaths.Value = 0
        credits.Value = 500
        print("New Player joined the game")
    else
        careerKills.Value = dataFromDatastore[1]
        careerDeaths.Value = dataFromDatastore[2]
        careerWins.Value = dataFromDatastore[3]
        careerLosses.Value = dataFromDatastore[4]
        credits.Value = dataFromDatastore[5]
        print("Data Loaded")
    end
end)

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

game:BindToClose(function()
	if game:GetService("RunService"):IsStudio() then return end
	
    for _,player in pairs(game.Players:GetPlayers()) do
		saveData(player)
    end
end)

--ADDS ONE TO TO THE PLAYERS CARRER ELIMINATIONS EVERY SECOND.
--THIS IS JUST FOR TESTING THAT THE DATASTORE WORKS
--DELETE AFTER YOU'RE DONE TESTING
spawn(function()
	while true do
		wait(1)
		for _, player in pairs(game.Players:GetPlayers())do
			player.leaderstats["Career Eliminations"].Value = player.leaderstats["Career Eliminations"].Value + 1
		end
	end	
end)

--AUTOSAVES EVERYONE'S DATA
while true do
	for _,player in pairs(game.Players:GetPlayers()) do
		saveData(player)
	end
    wait(60)
end

PS, only 4 of your values in the leaderstats will load. The values will be in the leaderstats and this script will work, but only four will show up on the leaderboard as you can see here. The solution to this would be to either use a custom leaderboard / use guis instead or don't display so many stats.

3 comments

0

yup this works perfectly, also am aware that only 4 show up just needed to put the values somewhere that i could see them to test

0

That's understandable.

0

I'm not sure why only four values show. Was it like that before?

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