Need help with DataStores

Hello! thank you so much in advance to anyone who actually reads all this and helps. I’m trying to make a simple tycoon game and right now I’m dealing with datastores. I am really confused.

I have the setting on in studio that lets you access datastore API within studio, so I think my script is the problem. The pcall’s aren’t erroring, because “success” is returning as true every time, but the datastores aren’t being set. This script should only run once since the point of it is to detect if the player has joined before, but it is running fully every time I press test. It seems like all the datastores are returning as Nil when I use GetAsync on any of them.

What am I doing wrong?

local dts = game:GetService("DataStoreService")


 -- Player data store initializer
game.Players.PlayerAdded:Connect(function(plr)
	local key = plr.UserId
	
	 -- Check if player has joined before
	local success = false
	local results = nil
	
	local count = 0
	while not success do
		success, results = pcall(function()
			dts:GetDataStore("JoinedBefore"):GetAsync(key)
		end)
		if success then break else
			count += 1
			warn(plr.Name.." failed has player JoinedBefore, retrying "..tostring(count))
			warn(results)
			task.wait(0.1)
		end
	end
	if results == nil then -- If player has not joined before
		
		 -- Player has not joined before, but now they have, set JoinedBefore to true
		success = false
		count = 0
		while not success do
			success, results = pcall(function()
				dts:GetDataStore("JoinedBefore"):SetAsync(key, true)
			end)
			if success then break else
				count += 1
				warn(plr.Name.." failed player JoinedBefore set to true, retrying "..tostring(count))
				warn(results)
				task.wait(0.1)
			end
		end
		
		 -- Player has not joined before, initialize data stores
		print(plr.Name.." has not joined before, initializing datastores")
		
		success = false
		count = 0
		while not success do
			success, results = pcall(function()
				dts:GetDataStore("TycoonUpgrades"):SetAsync(key, {-1})
			end)
			if success then break else
				count += 1
				warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
				warn(results)
				task.wait(0.1)
			end
		end
		
		success = false
		count = 0
		while not success do
			local success, results = pcall(function()
				dts:GetDataStore("Currency"):SetAsync(key, {
					Money = 0
				})
			end)
			if success then break else
				count += 1
				warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
				warn(results)
				task.wait(0.1)
			end
		end
		
		count = 0
		success = false
		while not success do
			success, results = pcall(function()
				dts:GetDataStore("Stats"):SetAsync(key, {
					MinutesPlayed = 0,
					TimesJoined = 0
				})
			end)
			if success then break else
				count += 1
				warn(plr.Name.." failed player datastore inizialization, retrying "..tostring(count))
				warn(results)
				task.wait(0.1)
			end
			
			print(plr.Name.." successfully initialized datastores")
		end
		
		
	end
end)
1 Like

I’d recommend directly returning the value inside the pcall statement like that:

local success, result = pcall(function ()
    return DataStore:GetAsync(key)
end)

if success then
-- result here will be your data
  else
-- result here will be an error
end

Also, Try not to use different datastores for things as simple as “Has Player Joined Already”, bad idea overall; use a single one with one key per player and a table to store the whole data.

3 Likes

That was the problem! I was setting the datastores correctly, I just didn’t know how to read them lol. thanks so much!

1 Like

exactly; pcall returns two values, first of which (named success by informal convention) returns true IF the code inside the passed function has NOT thrown an exception; it does not check anything else and does not depend on the result of the returned value

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.