DataStore issues

So, my DataStore script doesn’t work. I’m not sure if it’s having trouble saving or loading, but something’s wrong.

  1. Yes, DataStoring in Studio is enabled.
  2. I tried to see if it saved with the DataStore Editor plugin but I’m not so sure on how to use it. When it asked for a key, I was assuming it wanted the table index number.

oof

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Player_Data")

game.Players.PlayerAdded:connect(function(Player)
	local PlayerData
	
	local DataVars = Instance.new('Folder')
	DataVars.Name = "PlayerData"
	DataVars.Parent = Player
	
	local Money = Instance.new('IntValue',DataVars)
	Money.Name = "Money"
	
	local XP = Instance.new('IntValue',DataVars)
	XP.Name = "XP"
	
	------
	
	local CanSave = Instance.new('BoolValue',Player) 	                                               
	CanSave.Name = "CanSaveData"
	CanSave.Value = true
	
	local DataFetchSuccess, ErrorMessage = pcall(function()  
	                                                     
	PlayerData = DataStore:GetAsync(tostring(Player.UserId))
	end)
	
	if DataFetchSuccess then
		if PlayerData ~= nil then
			Money.Value = PlayerData[1]
			XP.Value = PlayerData[2]
		else
			Money.Value = 50
			XP.Value = 0
		end
	else
		Player.CanSaveData.Value = false
		Player:Kick("Your data didn't load! Please rejoin.")	
	end
end)

game.Players.PlayerRemoving:connect(function(Player)
	if Player.CanSaveData.Value == false then return end
		local PlayerData = {Player.PlayerData.Money.Value, Player.PlayerData.XP.Value,}
		
		local DataWriteSuccess, ErrorMessage = pcall(function()
			DataStore:SetAsync(tostring(Player.UserId), PlayerData)
		end)	
		
		if not DataWriteSuccess then
			local Retry_Count = 0
			
			while Retry_Count < 6 do
				wait(60)
				local Succeeded,Error = pcall(function()
				DataStore:SetAsync(tostring(Player.UserId), PlayerData)
			end)
			if Succeeded then break end
			Retry_Count = Retry_Count+1
		end
	end
end)

You should put print statements at each of the important parts and then see what doesn’t print but if everything prints you are most likely saving it wrong

1 Like

I found out that the money variable doesn’t save but the xp variable does… I put both of them in the table in order. At first, I thought maybe it worked like other languages did, with the 1st table entry always being 0. I tried making the 1s 2s and the 2s 3s but it didn’t work at all. I’m confused… oof

The way you’re doing it is wrong, DataFetchSuccess would be nil because you’re using it inside of the pcall when no value is returned yet and thus it’s still nil.

the key they are asking for is the player id

image
this might also be a problem


in the plugin they called it a scope

i click connect and it brings me to a page asking for a key
which im assuming is a table entry

No the key is what you had put in scope. Scope should be empty in your case. Player Id should go in the key section

Hmm… I’ve looked at other DataStore scripts and they called the PlayerId the scope.

Oh, well. I’ll try what you said.

Edit: Viewing the data works! Thanks. Still need to fix saving the money var though…

What is the money supposed to be at? and how are you changing it when you test. The values need to be changed serverside if you are doing it manually or else the script wont see the change

I’m changing the money value on the server side. My script just isn’t saving the first entry (money) for some reason.

do a print(Player.PlayerData.Money.Value) right before setAsync. Sometimes when you quit testing mode the player does not get removed before the server shuts down. Often times you gotta manually delete the player from game.Players to get the Player removing event to fire.

1 Like

It saved when I deleted the player from the game! Doesn’t make much sense because the second variable saved but the first one didn’t, but it works!

This will work in live servers, right?

1 Like

yessir
30 lettersssssssssssssssssssssssssssssssss