Issue with data store

I’m currently working on a rank system for my game and I have no idea why my script applies the data to the values but doesnt save the data when the value changed
My code:

local DSS = game:GetService("DataStoreService")
local dataStore = DSS:GetDataStore("Team_Ranks")

local function saveData(plr)
	local e = script.Parent.Players:FindFirstChild(plr.Name)
	local tableToSave = {
		e["Alpha Operations Division"].Value;
		e["Area Agency"].Value;
		e["Insurgency X"].Value;
		e["Internal Control Group"].Value;
		e["Medical Department"].Value;
		e["Mobile Task Force"].Value;
		e["Scientific Department"].Value;
		e["Security Department"].Value;
		e["Site Intelligence"].Value;
	}
	local success, errorMessage = pcall(dataStore.SetAsync, dataStore, plr.UserId, tableToSave)
	if success then
		print("Data saved")
	else
		print("Data not saved")
	end
end

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Wait()
	local t = script.Parent.Players.Template:Clone()
	t.Name =  p.Name
	t.Parent = script.Parent.Players
	wait()
	
	local data = nil

	local success, errorMessage = pcall(function()
		data = dataStore:GetAsync(p.UserId)
	end)

	if success and data then
		t["Alpha Operations Division"].Value = data[1]
		t["Area Agency"].Value = data[2]
		t["Insurgency X"].Value = data[3]
		t["Internal Control Group"].Value = data[4]
		t["Medical Department"].Value = data[5]
		t["Mobile Task Force"].Value = data[6]
		t["Scientific Department"].Value = data[7]
		t["Security Department"].Value = data[8]
		t["Site Intelligence"].Value = data[9]
	else
		print("Player has no data")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
	wait()
	script.Parent.Players:FindFirstChild(player.Name):Destroy()
end)

game:BindToClose(function()
	for _, player in ipairs(game.Players:GetPlayers()) do
		task.spawn(saveData, player)
	end
end)

The script is a server script inside of serverscriptservice
image

I’m not quite sure why you have put dataStore.SetAsync
I’m not entirely sure if the following code will work, because I don’t tend to use the DataStoreService often in coding, as it is mainly used for one thing.

The way I always use to save data is the following:

--this would go inside the saving function with the 'plr' parameter
local key = tostring("Player_"..plr.UserId)
local success, err = pcall(function()
    dataStore:SetAsync(key, tableToSave)
end)

if success then
    print("Saved!")
elseif not success and err then
    warn("Not saved! Will retry shortly.")
    saveData(plr)
end

I see two main problems with saving the script

  1. Like @12345koip said, you are using .SetAsync instead of :SetAsync
  2. Some people have issues with saving data in studio. Try testing it in game. You should also consider adding task.wait(3) in your :BindToClose function.

Edit: I just noticed another issue with your saving pcall. Do this instead:

dataStore:SetAsync(plr.UserId, tableToSave)

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