DataStore not working

Hey developers! So this might be a very nonsensical question but I read through my code and I couldn’t spot anything wrong. If you can please let me know. I was making a DataStore for a game that I’m working on and I made it so that if the Data Saves, it prints a string, and if it fails it prints another string. (I followed this from the Wiki I believe). Feedback would be much appreciated, thanks and cheers! :slight_smile:

To clarify: I should be seeing the strings printed in the output window but it’s completely blank.

Here’s the code written in the ServerScriptService on a Server Script.

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

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = leaderstats
	coins.Value = 0
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = leaderstats
	gems.Value = 0
	
	local rebirths = Instance.new("IntValue")
	rebirths.Name = "Rebirths"
	rebirths.Parent = leaderstats
	rebirths.Value = 0
	
	local PlayerID = "PlayerID_"..player.userId
	local DataFetching
	local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
		local DataFetching = DataStore:GetAsync(player.UserId)
	end)
	
	if SuccessfulFetch then
		coins.Value = DataFetching
		gems.Value = DataFetching
		rebirths.Value = DataFetching
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	
	local PlayerID = "PlayerID_"..player.UserId
	local CoinData = player.leaderstats.Coins.Value
	local GemData = player.leaderstats.Gems.Value
	local RebirthData = player.leaderstats.Rebirths.Value
	
	local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
		DataStore:SetAsync(PlayerID, CoinData, GemData, RebirthData)
	end)

	if SuccessfulFetch then
		print("I gots the Data! :D")
	else
		print("Oh noes, I failed to gets the data! D:")
		warn(UnsuccessfulFetch)
	end
end)

Hello!

You save your data on this format “PlayerID_”…player.userId,
But when you get the data, you are using player.UserId

On this line;
local DataFetching = DataStore:GetAsync(player.UserId)

Instead of “player.UserId” can you use “PlayerID” (which has been defined already but not being used)

2 Likes

I have also found another error which might be the cause,

On this line;
local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
DataStore:SetAsync(PlayerID, CoinData, GemData, RebirthData)
end)

Instead of that, use this:

DataStore:SetAsync(PlayerID, {CoinData, GemData, RebirthData})

The reason for that is because SetAsync() only accepts two parameters, the PlayerID and a value(in this case, table)

And on the PLAYERADDED FUNCTION replace the last lines with this;

if SuccessfulFetch then
	coins.Value = DataFetching[1]
	gems.Value = DataFetching[2]
	rebirths.Value = DataFetching[3]
end
2 Likes

seems you redeclare Data fetching, local is for declaring variable inside the scope,
you can remove the local thing and you never used UnsuccessfulFetch. try add else iside the if statement block. And inside that else statement, print this
print("Failed to fetch because of".. UnsuccessfulFetch)

1 Like

Change
local DataFetching = DataStore:GetAsync(player.UserId)
to
local DataFetching = DataStore:GetAsync(PlayerID)

and

if SuccessfulFetch then
		coins.Value = DataFetching
		gems.Value = DataFetching
		rebirths.Value = DataFetching
	end

to

if SuccessfulFetch then
		coins.Value = DataFetching[1]
		gems.Value = DataFetching[2]
		rebirths.Value = DataFetching[3]
	end

and
DataStore:SetAsync(PlayerID, CoinData, GemData, RebirthData)
to
DataStore:SetAsync(PlayerID, {CoinData, GemData, RebirthData})

1 Like

Hello!

Thank you for your advice, it’s quite helpful but I keep getting thrown an error when I replace the last lines of the player added function. See the below screenshot for the error. If you can tell me on how I can fix this would be greatly appreciated. :slight_smile:

image

Hello, I have redid the script myself, this should work as I have tested it.

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

    game.Players.PlayerAdded:Connect(function(player)
    	
    	local leaderstats = Instance.new("Folder")
    	leaderstats.Name = "leaderstats"
    	leaderstats.Parent = player
    	
    	local coins = Instance.new("IntValue")
    	coins.Name = "Coins"
    	coins.Parent = leaderstats
    	coins.Value = 0
    	
    	local gems = Instance.new("IntValue")
    	gems.Name = "Gems"
    	gems.Parent = leaderstats
    	gems.Value = 0
    	
    	local rebirths = Instance.new("IntValue")
    	rebirths.Name = "Rebirths"
    	rebirths.Parent = leaderstats
    	rebirths.Value = 0
    	
    	local PlayerID = "PlayerID_"..player.userId
    	local success, result = pcall(function()
    		return DataStore:GetAsync(PlayerID)
    	end)
    	
    	if result then
    		coins.Value = result[1]
    		gems.Value = result[2]
    		rebirths.Value = result[3]
    	end
    end)

    game.Players.PlayerRemoving:Connect(function(player)
    	
    	local PlayerID = "PlayerID_"..player.UserId
    	local CoinData = player.leaderstats.Coins.Value
    	local GemData = player.leaderstats.Gems.Value
    	local RebirthData = player.leaderstats.Rebirths.Value
    	
    	local SuccessfulFetch, UnsuccessfulFetch = pcall(function()
    		DataStore:SetAsync(PlayerID, {CoinData, GemData, RebirthData})
    	end)
    	
    	if SuccessfulFetch then
    		print("I gots the Data! :D")
    	else
    		print("Oh noes, I failed to gets the data! D:")
    		warn(UnsuccessfulFetch)
    	end
    end)
1 Like

Extremely sorry for the late response, but I would just like to thank you for helping me out here. I was stuck on this code block for some time now and I tested out your rewritten version of the code and it worked. Thank you very much once again and have a good one. :smile:

1 Like