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 save function for my Data Store Script not saving?

Hi, I have a data store script that gets data and saves data. But the saving part doesn't work. I've tested it by changed a player's cash value and added print functions on getting the data and saving the data. The print function on getting the data works but nothing for saving the data. No errors or etc was warned to the output. This is the script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("ServerDataStore")
local discordModule = require(game.ServerStorage.DiscordModule)

game.Players.PlayerAdded:Connect(function(player)
	local successValue = false
	for i = 1,3 do
		local success,data = pcall(function()
			return myDataStore:GetAsync(player.UserId)
		end)
		local leaderstats = Instance.new("Folder",player)
		leaderstats.Name = "leaderstats"

		local cash = Instance.new("IntValue",leaderstats)
		cash.Name = "Cash"

		local xp = Instance.new("IntValue",leaderstats)
		xp.Name = "Exp"

		if success then
			successValue = true
			if data then
				local table2 = data
				cash.Value = table2[1]
				xp.Value = table2[2]
				break
			else
				cash.Value = 0
				xp.Value = 0
				break
			end
		else
			warn("Data could not be retrieved! Retrying...")
		end
	end
	if not successValue then
		discordModule.sendMessage("Player_"..player.UserId.."'s data could not be retrieved!")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local successValue = false
	for i = 1,3 do
		local myTable = {}
		table.insert(myTable,1,player.leaderstats.Cash.Value)
		table.insert(myTable,2,player.leaderstats.Exp.Value)
		local success,errormessage = pcall(function()
			myDataStore:SetAsync(player.UserId,myTable)
		end)
		if not success then
			warn("Data could not be saved! Retrying... Error:",errormessage)
			return
		elseif success then
			successValue = true
			break
		end
	end
	if not successValue then
		discordModule.sendMessage("Player_"..player.UserId.."'s data could not be saved!")
	end
end)

Please Help!

1 answer

✓ Accepted answer

Just a suggestion. Maybe it works, but when you change the values for testing, you change them from the client. I have tried that many times before. In studio, you can both view it from the client and server perspective. If you are viewing from the client perspective, and you change the value in the explorer tab to 100, it will not actually be changed, and it will be saved as 0, and not 100. So the saving works, but it just saves the wrong values. If you want to test this script, you will have to switch to server perspective in the studio first, and then change the value. When I test this exact script in studio, (without the discord part) it works fine. Hope this helps

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