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)