✓ Accepted answer
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 :
- Do what, @TheMinecraftSmarts have said :
local data
local success, errormessage = pcall(function()
data = DataStore:GetAsync(key)
end)
- Check whether the data exists :
if success and data then
- Make a default value :
Coins.Value = data[1]
Coins.Value = 0
- If the data exists, give the coins saved in the data :
if success and data then
Coins.Value = data[1]
else
warn(errormessage)
end
- Add a
BindToClose function :
local RunService = game:GetService("RunService")
game:BindToClose(function()
if RunService:IsStudio() then return end
for _, plr in ipairs(game.Players:GetPlayers()) do
end
end)
What's the full output code after edits?
- For testing in Studio, it should be like :
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
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:BindToClose(function()
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)
- For Roblox Real server (during publish) :
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
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!
2 comments
Are you testing in studio if so thats why you have to test in the real game on roblox
errors?