Datastore problem

Hello,

so, I’ve looked on devform and tried using those to do this, but for some reason, they don’t work.

I want to save a table with data and get those from the datastore heres the script I made

  • Yes, the script is in server script
  • Yes, datastore is enabled in the game settings
local player_service = game:GetService("Players")
local data_store_service = game:GetService("DataStoreService")
local http_service = game:GetService("HttpService")

local player_data = data_store_service:GetDataStore("player_data")

local createValues = require(script.createValues)

function on_player_enter(player: Player)
	
	local folder_stats = Instance.new("Folder")
	folder_stats.Name = "leaderstats"
	folder_stats.Parent = player
	
	player.CharacterAppearanceLoaded:Once(function()
		
		local new_data = player_data:GetAsync(player.UserId)
		
		player_data:RemoveAsync(player.UserId)
		
		if new_data then
			new_data = http_service:JSONDecode(new_data)
			
			for _, v in pairs(new_data) do
				print(v)
			end
			
		end
		
		
--		player_data:RemoveAsync("player_data")
		
		local function on_death()
			player.Character.Humanoid.Died:Connect(function()
				--deaths.Value = deaths.Value + 1
			end)
		end
		
		on_death()
		
		
		player.CharacterAdded:Once(on_death)
		
	end)
	
end

local function on_player_leave(player: Player)
	
	local number_table = {}
	
	local leader_stats = player:FindFirstChild("leaderstats")
	
	if leader_stats then
		
		local deaths_value = leader_stats:FindFirstChild("deaths")
		local time_value = leader_stats:FindFirstChild("time")
		
		if deaths_value and time_value then
			
			local number_table = {
				["Time"] = 5,
				["Deaths"] = 5
			}
			
			warn("Saved data")
			
			player_data:SetAsync(player.UserId, http_service:JSONEncode(number_table))
		end
		
	end
	
end

player_service.PlayerRemoving:Connect(on_player_leave)
player_service.PlayerAdded:Connect(on_player_enter)
1 Like

Is there a particular reason why you are deleting the datastore immediately?


You should try moving the GetAsync stuff outside of the CharacterAppearanceLoaded event.


If you add print statements throughout the code, which points in the code are reached and which ones aren’t?

1 Like

it was there because the data wouldn’t change so I put that there so it would clear it and let it get changed

1 Like

SetAsync will overwrite existing data. Don’t have the RemoveAsync part in the code.

(Can you please address the 2 other parts in my reply?)

1 Like

I changed and I didn’t sleep well so I guess I wasn’t thinking because it was a mess, heres a updated one, how can I get the name of the value?

local player_service = game:GetService("Players")
local data_store_service = game:GetService("DataStoreService")
local http_service = game:GetService("HttpService")

local player_data = data_store_service:GetDataStore("player_data")

local createValues = require(script.createValues)

function on_player_enter(player: Player)
	
	local folder_stats = Instance.new("Folder")
	folder_stats.Name = "leaderstats"
	folder_stats.Parent = player

	local new_data = player_data:GetAsync(player.UserId)

	if new_data then
		
		print("passed if new_data data")
		
		new_data = http_service:JSONDecode(new_data)

		for _, v in pairs(new_data) do
			print(v)
		end

	end

end

local function on_player_leave(player: Player)
	
	local number_table = {}
	
	local leader_stats = player:FindFirstChild("leaderstats")
	
	if leader_stats then
			
		local number_table = {
			name = 9,
			deaths = 10
		}
			
		warn("Saved data")
			
		player_data:SetAsync(player.UserId, http_service:JSONEncode(number_table))
		
	end
	
end

player_service.PlayerRemoving:Connect(on_player_leave)
player_service.PlayerAdded:Connect(on_player_enter)
1 Like

Assuming it is outputting from the print statement found here:

you would be able to access individual things by doing

new_data.name

and

new_data.deaths
2 Likes

FYI, you do not need to use GetAsync and RemoveAsync consecutively. Much like table.remove, RemoveAsync will return what it has removed from the database. You can combine both getting and removing into one operation. You do not need to encode your data to JSON either. Roblox already encodes all data uploaded to the database, so just upload the raw table

1 Like

I got that from an other devforum post so I thought I needed that.

1 Like

Figured. Most people don’t know how to implement data persistence in an efficient and effective manner. There is much more to data persistence than what DataStoreService provides alone. This is why so many wrappers exist

2 Likes

I’m new to datastore but how would I go about passing a raw table?

1 Like

Exactly how you would anything else—just drop the JSON-encoding code

1 Like

What? I’m lost, what do you mean by “drop the json-encoding code”

1 Like

Where in your script do you think you’re doing something along the lines of “JSON-encoding”…?

1 Like

I changed mostly the entire script

I removed http service because you said datastoreservice already does that and changed the table naming

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

local player_data = data_store_service:GetDataStore("player_data")

local createValues = require(script.createValues)

function on_player_enter(player: Player)
	
	local folder_stats = Instance.new("Folder")
	folder_stats.Name = "leaderstats"
	folder_stats.Parent = player
	
	local time_value, death_value = createValues:create_leader_stats(player, folder_stats)

	local new_data = player_data:GetAsync(player.UserId)

	if new_data then

		for i, v in pairs(new_data) do
			
			if i == "Time" then
				time_value.Value = v
				
			end
			
			if i == "Deaths" then
				death_value.Value = v
			end
			
		end

	end

	local function on_death()
		death_value.Value = death_value.Value + 1
	end

	player.CharacterAppearanceLoaded:Connect(function()
		player.Character.Humanoid.Died:Connect(on_death)
	end)

end

local function on_player_leave(player: Player)
	
	local number_table = {}
	
	local leader_stats = player:FindFirstChild("leaderstats")
	
	if leader_stats then
			
		local number_table = {}
			
		number_table["Time"] = leader_stats:FindFirstChild("time_value").Value
		number_table["Deaths"] = leader_stats:FindFirstChild("death_value").Value
			
		player_data:SetAsync(player.UserId, number_table)
		
	end
	
end

player_service.PlayerRemoving:Connect(on_player_leave)
player_service.PlayerAdded:Connect(on_player_enter)

thanks to @Ziffixture and @SeargentAUS for helping me fix this

1 Like

This can be simplified to just

time_value.Value = new_data["Time"]
death_value.Value = new_data["Deaths"]

CharacterAppearanceLoaded is not needed here. Nor do you need FindFirstChild to access those values in the player

whenever I use CharacterAppearanceLoaded it’ll fire everytime the player respawns or spawns in for the first time so I have it to set the death trigger

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