Datastore for my Tower Defense Game

Hello, I need help / Review on my datastore that i use on my Tower Defense Game. i want to know y’all feedback about my datastore, is it good enough or not. but sometimes player loses their data with no error.?

this is a script for the SaveData, (im using UpdateAsync)

function PlayerSaveData(plr)
	local key = plr.UserId

	local dataTable = {}

	for i, v in pairs(plr.leaderstats:GetChildren()) do
		dataTable[v.Name] = v.Value
	end

	local success, err = pcall(function()
		PlayerDatas:UpdateAsync("plr-" .. plr.UserId, function(oldValue)
			local newValue = oldValue or nil
			newValue = dataTable
			return newValue
		end)
	end)

	if success then
		event:FireAllClients(plr, "Game Controller", "Your data has been saved | "..tostring(success))
		print(plr.Name.." data has been saved.")
	else
		event:FireClient(plr, "Game Controller", "Failed to save your data | "..tostring(err))
		print(plr.Name.." failed to save this player data.")
	end
end

and this is script to get their data (Using UpdateAsync too):

function LeaderstatsLoad()
    local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local Coins = Instance.new("IntValue",folder)
	Coins.Name = "Coins"
	
	local Diamonds = Instance.new("IntValue", folder)
	Diamonds.Name = "Diamonds"
	
	local TowerEquipped = Instance.new("IntValue", plr)
	TowerEquipped.Name = "TowerEquipped"
	
	local coinDatas
	
	local success, err = pcall(function()
		coinDatas = PlayerDatas:UpdateAsync("plr-" .. plr.UserId, function(oldValue)
			local newValue = oldValue or nil
			newValue = oldValue
			return newValue
		end)
	end)

	if success then
		print("Successfully load player data"..tostring(success), plr.Name)
	else
		print("Failed to load player data"..tostring(err), plr.Name)
	end

	if coinDatas then
		for data, DataValue in pairs(coinDatas) do
			if folder:FindFirstChild(data) then
				folder[data].Value = DataValue
			end
		end
	end
	
	spawn(function() -- Automaticly save their data every 60 seconds
		while true do
			PlayerSaveData(plr) -- run SaveData Function
			wait(60)
		end
	end)

thanks.

1 Like
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local Stats = DataStoreService:GetDataStore("stats")

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

	local Coins = Instance.new("IntValue",folder)
	Coins.Name = "Coins"
	Coins.Parent = folder

	local Diamonds = Instance.new("IntValue", folder)
	Diamonds.Name = "Diamonds"
	Diamonds.Parent = folder

	local TowerEquipped = Instance.new("IntValue", plr)
	TowerEquipped.Name = "TowerEquipped"
	TowerEquipped.Parent = folder
	
	local key = "plr-".. player.userId
	local SavedData = Stats:GetAsync(key)

	if SavedData then

		
		Coins.Value = SavedData[1]
		Diamonds.Value = SavedData[2]
		TowerEquipped.Value = SavedData[3]
		
	else
		local DataToSave = {Coins.Value, Diamonds.Value, TowerEquipped.Value}
		Stats:SetAsync(key, DataToSave)
	end

	
	
	
end)

Players.PlayerRemoving:Connect(function(player)
	local df = game.Workspace:WaitForChild(player.Name)
	df:Destroy()


	local key = "plr-".. player.userId
	local DataToSave = {player.stats.Coins.Value, player.stats.Diamonds.Value, player.stats.TowerEquipped.Value}
	Stats:SetAsync(key, DataToSave)
end)

Try this and if you want to change the value of get it just do player.leaderstats.Coins.Value

2 Likes

This seems like a fairly irrelevant post & you’ve likely just copied and pasted this script from elsewhere.

Yea this is my script from my game but if you want i can write some simple one

(and this is my scripting style)

local daastores = game:GetService("DataStoreService")
local stats = daastores:GetDataStore("stats")

game.Players.PlayerAdded:Connect(function(player)
	local Folder = Instance.new("Folder")
	Folder.Parent = player
	Folder.Name = "leaderstats"
	local int = Instance.new("IntValue")
	int.Parent = Folder
	int.Value = 0
	int.Name = "int"
	local saveddata = stats:GetAsync(player.Name)
	if saveddata then		
		int.Value = saveddata[1]	
	else
		stats:SetAsync(player.Name, {int.Value})
	end
end)
game.Players.PlayerRemoving:Connect(function(player)	
		stats:SetAsync(player.Name, {player.leaderstats.int.Value})	
end)


2 Likes

I’ve already try this kind of datastore before, but still sometimes player lose their data.

When does this problem happen because for me its working fine even if i alf + F4

Now i found the issue with the datastore, thanks for helping me.

1 Like

No problem and if you are still using SetAsync use UpdateAsync function it’s better and Merry Christmas