Which script is better?

I have a script and when I showed it to GPT chat for another problem and it said I made these changes too
But I feel this is not very secure and has some problems
I have put both versions for you to look at

--//Datastore Script:

--//Datastore
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Test")

--//Function
local function saveData(player)
	local tableToSave = {
		player.leaderstats.Cash.Value,
		player.leaderstats.Coin.Value,
		player.leaderstats.Diamond.Value,
		player.leaderstats.Level.Value,
	}

	local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, tableToSave)

	if success then
		print("Data has been saved!")
	else
		print("Data has not been saved!")
	end
end

--//leaderstats
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Wait()
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Parent = leaderstats
	cash.Value = 0
	
	local coin = Instance.new("IntValue")
	coin.Name = "Coin"
	coin.Parent = leaderstats
	coin.Value = 0

	local level = Instance.new("IntValue") -- مقدار جدید Level
	level.Name = "Level"
	level.Parent = leaderstats
	level.Value = 0 -- مقدار پیش‌فرض برای Level
	
	local diamond = Instance.new("IntValue")
	diamond.Name = "Diamond"
	diamond.Parent = leaderstats
	diamond.Value = 0
	
	local data = nil

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

	if success and data then
		cash.Value = data[1]
		coin.Value = data[2]
		level.Value = data[3] --or 1 -- اگر مقدار Level موجود نبود مقدار پیش‌فرض 1 بده
		diamond.Value = data[4]
	else
		print("The Player has no Data!")
		warn(errorMessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	saveData(player)
end)

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

And now the GPT chat script:

--//Datastore Script

local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("Test")

local function saveData(player)
	local tableToSave = {
		player.leaderstats.Cash.Value,
		player.leaderstats.Coin.Value,
		player.leaderstats.Diamond.Value,
		player.leaderstats.Level.Value
	}

	local success, errorMessage = pcall(function()
		dataStore:SetAsync(player.UserId, tableToSave)
	end)

	if success then
		print("Data has been saved for", player.Name)
	else
		warn("Failed to save data for", player.Name, errorMessage)
	end
end

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

	local cash = Instance.new("IntValue", leaderstats)
	cash.Name = "Cash"

	local coin = Instance.new("IntValue", leaderstats)
	coin.Name = "Coin"

	local diamond = Instance.new("IntValue", leaderstats)
	diamond.Name = "Diamond"

	local level = Instance.new("IntValue", leaderstats)
	level.Name = "Level"

	local success, data = pcall(function()
		return dataStore:GetAsync(player.UserId)
	end)

	if success and data then
		cash.Value = data[1] or 0
		coin.Value = data[2] or 0
		diamond.Value = data[3] or 0
		level.Value = data[4] or 0
	else
		print("The Player has no Data!")
	end
end)

game.Players.PlayerRemoving:Connect(saveData)

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

ChatGPT’s script works the same. However, it is not up to today’s standards.

As for your code, you should not assume that the player’s character will be unavailable at the time of Players.PlayerAdded—you may cause an infinite yield. On that same note, you do not seem to need the character, so you should drop that line of code

2 Likes

Both aren’t good, because it suffers from a (surprisingly) common mistake. In the event the GetAsync() fails, the cash, coin, diamond, and level value objects will have default values. Then, when saveData is called, those default values are saved, overwriting the user’s data, completely wiping it (although, datastores have a feature to retrieve an older version of the data)

I talk more in detail about this and protection against race conditions with datastores in this thread

2 Likes

Data match order and safer for debugging, and fixed 4 to a 3 bug. sweet

1 Like

whichever one wasn’t edited by AI

1 Like