I tried many ways to do datastore, I Enable Api Services, I watch many tutorial, I read Roblox Documentation but still doesn’t. Can anyone help me?
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
end)
local function savePlayerData(player)
local cashValue = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, error = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. error)
end
end
end
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
The reason this doesn’t work is because the game closes before the data saves.
An easy fix is to tell the game to delay 5 seconds before it shuts down.
Put this before any functions in your script:
game.BindToClose:Connect(function() --Runs the code inside before the game closes.
task.wait(5)
end)
Also instead of relying solely on game:BindToClose , you should consider saving player data at regular intervals, like when they make changes to their cash.
here’s how you can modify the savePlayerData function to save data more frequently:
local function savePlayerData(player)
local cashValue = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, error = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. error)
end
end
end
-- Call savePlayerData whenever the player's cash changes
-- For example, you can put this in a script that listens to a player's actions
Cash.Changed:Connect(function(newValue)
savePlayerData(player)
end)
-- Also save data when the player is leaving
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
I tried this and it completely works, because I added the :BindToClose(), it’s something with his script that doesn’t do data saving correctly.
Making it change every time the Cash value changes could be consistent, but if you’re continuosly getting cash, then that can be a problem.
You might say, “just don’t get lots of cash at a time, to slow down the data store queue”, but that’s a big limitation. I’d save data every time the player leaves the game.
Oh you’re right, ig we should just focus on when the player leaves.
this might work
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
end)
local function savePlayerData(player)
local cashValue = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, error = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Uh-oh, we couldn't save " .. player.Name .. "'s data: " .. error)
end
end
end
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
game:BindToClose(function()
for _, player in pairs(Players:GetPlayers()) do
savePlayerData(player)
end
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
game:BindToClose(function()
task.wait(5)
end)
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
game:BindToClose(function()
task.wait(5)
end)
local function savePlayerData(player)
local cashValue = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
if cashValue then
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local key = "Player_" .. player.UserId
local success, error = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. error)
end
end
end
-- Call savePlayerData whenever the player's cash changes
-- For example, you can put this in a script that listens to a player's actions
game:BindToClose(function()
task.wait(5)
end)
Cash.Changed:Connect(function(newValue)
savePlayerData(player)
end)
game:BindToClose(function()
task.wait(5)
end)
-- Also save data when the player is leaving
Players.PlayerRemoving:Connect(function(player)
savePlayerData(player)
end)
end)
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local CASH_DATA_STORE_NAME = "PlayerCashData"
local dataStore = DataStoreService:GetDataStore(CASH_DATA_STORE_NAME)
local function savePlayerData(player)
local cashValue = player:FindFirstChild("leaderstats"):FindFirstChild("Cash")
if cashValue then
local key = "Player_" .. player.UserId
local success, err = pcall(function()
dataStore:SetAsync(key, cashValue.Value)
end)
if not success then
warn("Error saving player data for " .. player.Name .. ": " .. err)
end
end
end
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Cash = Instance.new("NumberValue")
Cash.Name = "Cash"
Cash.Value = 0
Cash.Parent = leaderstats
local key = "Player_" .. player.UserId
local data
local success, err = pcall(function()
data = dataStore:GetAsync(key)
end)
if success then
Cash.Value = data
end
Cash.Changed:Connect(function()
savePlayerData(player)
end)
end)
Players.PlayerRemoving:Connect(savePlayerData)
game:BindToClose(function()
task.wait(3)
end)