script:
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("PlayerData") game.Players.PlayerAdded:Connect(function(plr) local key = plr.UserId.."Key" local success, data = pcall(function() return DataStore:GetAsync(key) end) local folder = Instance.new("Folder", plr) folder.Name = "stats" local Coins = Instance.new("NumberValue", folder) Coins.Name = "Coins" Coins.Value = data[1] print(data[1]) workspace.Coin.OnServerEvent:Connect(function(plr, add) Coins.Value = Coins.Value + 1 end) end) game.Players.PlayerRemoving:Connect(function(plr) local key = plr.UserId.."Key" local data = { plr.stats.Coins.Value } local success, err = pcall(function() DataStore:SetAsync(key, data) end) end)
i think the problem is in the part when the player leaves the game, but I don't know how to solve it
Well, there are few flaws in your datastore code.
Flaws:
You are assuming a string as a table, which is data
.
You are not assigning a variable for the returning function in pcall()
.
There is no BindToClose
function.
You are not checking whether the data exists or not (for new players).
Possible solutions :
local data local success, errormessage = pcall(function() data = DataStore:GetAsync(key) end)
if success and data then -- It will ensure whether the data is not nil and exists
-- Instead of Coins.Value = data[1] -- Make it Coins.Value = 0 -- or whatever value you want to have for new players
if success and data then Coins.Value = data[1] else warn(errormessage) end
BindToClose
function :local RunService = game:GetService("RunService") game:BindToClose(function() if RunService:IsStudio() then return end for _, plr in ipairs(game.Players:GetPlayers()) do -- Your code for saving data end end)
What's the full output code after edits?
local RunService = game:GetService("RunService") local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("PlayerData") game.Players.PlayerAdded:Connect(function(plr) local key = plr.UserId.."Key" local data local success, errormessage = pcall(function() data = DataStore:GetAsync(key) end) local folder = Instance.new("Folder", plr) folder.Name = "stats" local Coins = Instance.new("NumberValue", folder) Coins.Name = "Coins" Coins.Value = 0 -- As example print(data[1]) if success and data then Coins.Value = data[1] else warn(errormessage) end workspace.Coin.OnServerEvent:Connect(function(plr, add) Coins.Value = Coins.Value + 1 end) end) --[[ game.Players.PlayerRemoving:Connect(function(plr) local key = plr.UserId.."Key" local data = { plr.stats.Coins.Value } local success, err = pcall(function() DataStore:SetAsync(key, data) end) end) ]] game:BindToClose(function() -- if RunService:IsStudio() then return end for _, plr in ipairs(game.Players:GetPlayers()) do local key = plr.UserId.."Key" local data = { plr.stats.Coins.Value } local success, err = pcall(function() DataStore:SetAsync(key, data) end) end end)
local RunService = game:GetService("RunService") local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("PlayerData") game.Players.PlayerAdded:Connect(function(plr) local key = plr.UserId.."Key" local data local success, errormessage = pcall(function() data = DataStore:GetAsync(key) end) local folder = Instance.new("Folder", plr) folder.Name = "stats" local Coins = Instance.new("NumberValue", folder) Coins.Name = "Coins" Coins.Value = 0 -- As example print(data[1]) if success and data then Coins.Value = data[1] else warn(errormessage) end workspace.Coin.OnServerEvent:Connect(function(plr, add) Coins.Value = Coins.Value + 1 end) end) game.Players.PlayerRemoving:Connect(function(plr) local key = plr.UserId.."Key" local data = { plr.stats.Coins.Value } local success, err = pcall(function() DataStore:SetAsync(key, data) end) end) game:BindToClose(function() if RunService:IsStudio() then return end for _, plr in ipairs(game.Players:GetPlayers()) do local key = plr.UserId.."Key" local data = { plr.stats.Coins.Value } local success, err = pcall(function() DataStore:SetAsync(key, data) end) end end)
Lemme know if it helps!
I think the second parameter of a pcall is reserved for error message codes. Why don't you try this:
local data local success, errormessage = pcall(function() data = DataStore:GetAsync(key) end)
Two things -
1) Are you testing in studio if so thats why you have to test in the real game on roblox
2) you should have it do this
local folder = Instance.new("Folder", plr) folder.Name = "stats" local Coins = Instance.new("NumberValue", folder) Coins.Name = "Coins" local success, data = pcall(function() return DataStore:GetAsync(key) end) if success then Coins.Value = data[1] end