Datastore issue

Hey, I am having a bug with my datastore. It keeps printing out “data has not been saved”

Which is odd, I do not see how it cannot save. Anyways let me know.

Code:

local DataStore = game:GetService("DataStoreService")

local DS = DataStore:GetDataStore("GemsData")


Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Value = DS:GetAsync(player.UserId) or 0
	Gems.Parent = leaderstats
	
	local success, errormsg = pcall(function()
		DS:GetAsync(player.UserId)
	end)
	
	if success then
		print(player.Name.."'s data has been found.")
	else
		print(player.Name.."'s data has not been found.")
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player:WaitForChild("leaderstats")
	local Gems = leaderstats:WaitForChild("Gems")
	
	
	local success,errormsg = pcall(function()
		DS:SetAsync(player.UserId, Gems.Value)
	end)
	
	if success then
		print(player.Name.."'s data has been saved.")
	else
		print(player.Name.."'s data has not been saved.")
	end
	
end)```

Change Gems:SetAsync(player.UserId, Gems.Value) to DS:SetAsync(player.UserId, Gems.Value).

Why? Well because you are trying to execute :SetAsync() on a member of leaderstats. :SetAsync is a method that can only be executed upon a datstore, such as DS (as written in your script).

Hey, thanks for your answer. But now it doesnt print at all sadly. It doesnt print “data has not been found”, I debugged it. After the setasync it doesnt print anything anymore.

what is the errormsg you’re getting?

Try this:

local DataStore = game:GetService("DataStoreService")
local DS = DataStore:GetDataStore("GemsData")
local Players = game:GetService("Players") 

local function GetData(player)
	local tries = 0	
	local success
	local Gems
	repeat
		tries = tries + 1
		success = pcall(function() Gems=DS:GetAsync(player.UserId) end)
		if not success then wait(3) end
	until tries == 3 or success
	if not success then warn("Cannot Read Player Data!") return false end
	return Gems

end

local function SaveData(player)
	local tries = 0	
	local success
	local Gems = player:WaitForChild("leaderstats"):WaitForChild("Gems").Value
	repeat
		tries = tries + 1
		success = pcall(function() DS:SetAsync(player.userId, Gems)end)
		if not success then wait(3) end
	until tries == 3 or success
	if not success then warn("Cannot Save Player Data!") return false end


end

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Value = GetData(player) or 0
	Gems.Parent = leaderstats
end)	

Players.PlayerRemoving:Connect(function(player)
	SaveData(player)
end)

didnt work, nothing printed out.

It will only show output if there is an error in getting or saving the data.
I just tested the code I gave you (adding 10 to gems value before leaving the game) and it’s works fine:
First run Gems=0 as I’m a new player with no data saved
second run Gems=10 because it saved the data when I left the first time.

Well my data still doesn’t save.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")

local DataStore = DataStoreService:GetDataStore("your data store")

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Value = 0
	gems.Parent = leaderstats

	local ok, result = pcall(DataStore.GetAsync, DataStore, player.UserId)

	if ok then
		if result then
			gems.Value = result
		end
		warn(player.Name .. "'s data loaded")
	else
		warn("failed to get " .. player.Name .. "'s data due to " .. result)
	end
end)

Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player.leaderstats

	local ok, result = pcall(DataStore.SetAsync, DataStore, player.UserId, leaderstats.Gems.Value)

	if ok then
		print(player.Name .. "'s data has been saved!")
	else
		warn("failed to save " .. player.Name .. "'s data due to: " .. result)
	end
end)

not tested btw but it should work.

Okay for some reason it doesnt work in studio but it does in-game. (Yes API and HTTP requests are enabled) anyways thank you.

1 Like

Yeah, so another thing you should learn is always playtest your game in Roblox when testing data storings.