Issue with datastore

So I was working on a rank system for my game, and I wanted to use data stores to save data and load, but I found a problem. Data never got saved, and no errors ever came up, I don’t know why, but It’s not working. I also have API requests enabled.

Code:

local DSS = game:GetService("DataStoreService")
local RankDS = DSS:GetDataStore("RankDataStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local levelValue = Instance.new("IntValue") 
	levelValue.Name = "Rank"
	levelValue.Parent = leaderstats
	
	local expValue = Instance.new("NumberValue")
	expValue.Name = "expValue"
	expValue.Parent = plr
	
	local maxExp = Instance.new("NumberValue")
	maxExp.Name = "maxExp"
	maxExp.Parent = plr
	

	local function LevelUp()
		levelValue.Value += 1
		expValue.Value = 0
		maxExp.Value = 1000
		
	end

	local function GainExp(gain)
		expValue.Value += gain
		if expValue.Value > maxExp.Value then
			LevelUp()
		end
	end
	
	while wait(1) do
		GainExp(100)
	end
	
	--Load data
	
	local PlrId = "Player_"..plr.UserId
	print("First ID var has been set.")
	
	
	local data
	local success, errormessage = pcall(function()
		data = RankDS:GetAsync(PlrId)
	end)
	print("First pcall has been fired.")
	
	if success then
		print("Success = true!")
		levelValue.Value = data
	end
	
end)

--Save data

game.Players.PlayerRemoving:Connect(function(plr)
	local PlrId = "Player_"..plr.UserId

	
	local data = plr.leaderstats.Rank.Value
	print("Second variables has been set!")
	
	local success, errormessage = pcall(function()
		RankDS:UpdateAsync(PlrId,data)
	end)
	print("Second pcall has been fired!")
	
	if success then
		print("Data has been saved successfully")
	else
		print("There has been an error")
		warn(errormessage)
	end
		
end)

Any help is appreciated!

1 Like

What appears in your output? Does it all print?

image

just this, no errors, nothing.

You are printing the player with the plr.UserId. Perhaps remove the printing from the UserID.

Where am I printing it? I can’t seem to find it?

This could mess up the datastores from saving and loading. I would suggest just removing it.

1 Like

So I just do local PlrId = plr.UserId?

1 Like

yo need to publish your game, is it published?

Yeah, you can try that and see if it works.

And also, yeah, publish the game but I’m sure you got that.

Yes actually, though it’s on a different account than the one on my dev forum page.

Hi, I think the data should be saved like this when you use UpdateAsync()

local data = {
Rank = plr.leaderstats.Rank.Value
}

Also, I think that is not the correct way to use UpdateAsync()

You should use it like this:

RankDS:UpdateAsync(PlrId, function(OldStats)
    return data
end)
1 Like

I tried, and the same thing happened.

image

Maybe you’re using UpdateAsync() wrong like @AxeI_c said. It seems like you are doing SetAsync() but UpdateAsync() works differently. You can do it how @AxeI_c said or you can try using SetAsync().

2 Likes

I spoke too soon, It doesn’t load the data, just saves it. I still want to use UpdateAsync() over SetAsync() though.

I think it may be your while loop, the code will keep repeating the loop and never continue. Try this:

spawn(function()
    while wait(1) do
	    GainExp(100)
	end
end)

UpdateAsync requires a function as the second argument rather than a value: GlobalDataStore | Roblox Creator Documentation

That fixed it, kind of. My data did save and load yes, but even tho I set my rank to 4, it keeps setting my rank to 2 not 4, even though I set my rank to 4 on the server.

Any ideas as to why this is happening?

You need to test it in a live server.

The Studio server is shut down before it is saved.

1 Like

Sometimes data doesn’t save in studio. Try saving in game.

1 Like