Is anything wrong

Is anything wrong in this datastore script?

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

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local Coins = Instance.new("IntValue", leaderstats)
	Coins.Name = "Coins"
	
	local Gems = Instance.new("IntValue", leaderstats)
	Gems.Name = "Gems"
	
	local Rebirths = Instance.new("IntValue", leaderstats)
	Rebirths.Name = "Rebirths"
	
	local playerUserID = "Player_"..player.UserId
	
	-- Load Data
	local data
	local success, errormessage = pcall(function()
		DataStore:GetAsync(playerUserID)
	end)
	
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = "Player_"..player.UserId
	
	local data = {
		Coins = player.leaderstats.Coins.Value;
		Gems = player.leaderstats.Gems.Value;
		Rebirths = player.leaderstats.Rebirths.Value;
	}
	
	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserID, data)
	end)
	
	if success then
		print("Successfully Saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
	
end)

All help appreciated.

After this part,
you’ll need to check if there is data, and set the gems, coins and rebirths values accordingly to their values in the data.

For save the data, you need transform the table into a JSON array using http service

And when you go to use the data, you need use http service again with JSONDecode method

Because the second value returned of “GetAsync” is the stored value with “SetAsync”, not an error message

Which part shall i write that in?

before storing the data with “SetAsync”

But there is no data variable in the function. Look:

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserID = "Player_"..player.UserId
	
	local data = game.HttpService:JSONEncode(data)
	local success, errormessage = pcall(function()
		DataStore:SetAsync(playerUserID, data)
	end)
	
	if success then
		print("Successfully Saved!")
	else
		print("There was an error!")
		warn(errormessage)
	end
	
end)

“SetAsync” not accept tables with value, JSONEncode transforms the table in a string, and JSONDecode transforms that string in an array again. You need modify now the “GetAsync” function, because the second value returned of “GetAsync” function is not a error message. You can do this with your code:

Error on:

Error:
Argument 1 missing or nil.

Transforming tables into the JSON format is not required

It’s correctly, I’m sorry. Then we go to use if-then, to avoid the error. Can be like this:

So “SetAsync” accepts tables and arrays?

No, because this code doesn’t work, and the ‘code review’ section is for codes that do work

Yes; I always just send raw tables through and it works fine

1 Like

image

They do?

urda:SetAsync({"hi"})

you can substitute:

for:

	Coins.Value =  0
	Gems.Value =  0
	Rebirths.Value = 0

if data ~= nil then
		data = game.HttpService:JSONDecode(data)
		Coins.Value = data.Coins
		Gems.Value = data.Gems
		Rebirths.Value = data.Rebirths
	end

It’s correctly, but I started this example using JSON

I fixed it so i dont need help anymore

Sorry for the delay, but I now have the error in the script.
The argument is nil because in the function:

the return instruction for the function “GetAsync” has not been given.

We need write “return” at the line:

local success, data = pcall(function()
-- in this part we need write "return"
		return DataStore:GetAsync(playerUserID)
	end)

Look at this post please! I already…