Hello
I’m pretty new to Datastores and recently started experimenting with them a bit. I always was working with same script. At the beginning it worked fine. But when i added the most recent data to store, it stopped working. I’ve already asked my friends but noone got a clue. I’ve also gone trough the forum and not really found an answer.
(its not saving all 3, getting the data is no problem)
the script is following:
local Player = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local StationsDS = DataStoreService:GetDataStore("Stations")
local MoneyDS = DataStoreService:GetDataStore("Money")
local PLAYERS_LEFT = 0
local BindableEvent = Instance.new("BindableEvent")
local function CreateLeaderBoard(player)
PLAYERS_LEFT += 1
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local points = Instance.new("IntValue")
points.Name = "Stations"
points.Value = 0
points.Parent = leaderstats
local money = Instance.new("IntValue")
money.Name = "Money"
money.Value = 0
money.Parent = leaderstats
local MetroLiveryBought = Instance.new("BoolValue")
MetroLiveryBought.Parent = game.Players:FindFirstChild(player.name)
MetroLiveryBought.Name = "MetroLiveryBought"
-- get Data
local StationsValue = game.Players:FindFirstChild(player.Name).leaderstats.Stations
local StationsFromDS
local success, errormessage = pcall(function()
StationsFromDS = StationsDS:GetAsync(player.UserId .. "-Stations")
end)
if success then
StationsValue.Value = StationsFromDS
print("Playerdata for Stations from ".. player.name .. " successfully recovered.")
else
warn("There has been an error while getting Stations data for ".. player.name .. " " .. errormessage)
end
-- get money
success = nil
errormessage = nil
local MoneyValue = game.Players:FindFirstChild(player.name).leaderstats.Money
local MoneyFromDS
local success, errormessage = pcall(function()
MoneyFromDS = MoneyDS:GetAsync(player.UserId .. "-Money")
end)
if success then
MoneyValue.Value = MoneyFromDS
print("Playerdata for Money from ".. player.name .. " successfully recovered.")
else
warn("There has been an error while getting Money data for ".. player.name .. " " .. errormessage)
end
-- get if it has been bought (Metro Livery)
success = nil
errormessage = nil
local MetroLiveryBought = game.Players:FindFirstChild(player.name).MetroLiveryBought
local MetroLiveryFromDS
local success, errormessage = pcall(function()
MetroLiveryFromDS = MoneyDS:GetAsync(player.UserId .. "-MetroLivery")
end)
if success then
MetroLiveryBought.Value = MetroLiveryFromDS
print("Playerdata for Metro Livery from ".. player.name .. " successfully recovered.")
else
warn("There has been an error while getting Metro Livery data for ".. player.name .. " " .. errormessage)
end
end
local function SaveData(player)
-- save Data
local StationsValue = game.Players:FindFirstChild(player.Name).leaderstats.Stations
local MoneyValue = game.Players:FindFirstChild(player.name).leaderstats.Money
local StationsFromDS
local success, errormessage = pcall(function()
StationsFromDS = StationsDS:SetAsync(player.UserId .. "-Stations", StationsValue.Value)
end)
if success then
print("Playerdata from ".. player.name .. " successfully saved.")
else
warn("There has been an error while saving data for ".. player.name .. " " .. errormessage)
end
success = nil
errormessage = nil
-- save Money
local MoneyFromDS
local success, errormessage = pcall(function()
MoneyFromDS = MoneyDS:SetAsync(player.UserId .. "-Money", MoneyValue.Value)
end)
if success then
print("Playerdata from ".. player.name .. " successfully saved.")
else
warn("There has been an error while saving data for ".. player.name .. " " .. errormessage)
end
-- save Metro livery
success = nil
errormessage = nil
local MetroLiveryBought = player.MetroLiveryBought
local MetroLiveryFromDS
local success, errormessage = pcall(function()
MetroLiveryFromDS = MoneyDS:SetAsync(player.UserId .. "-MetroLivery", MetroLiveryBought.Value)
end)
if success then
MetroLiveryBought.Value = MetroLiveryFromDS
print("Playerdata for Metro Livery from ".. player.name .. " successfully saved.")
else
warn("There has been an error while saving Metro Livery data for ".. player.name .. " " .. errormessage)
end
PLAYERS_LEFT -= 1
BindableEvent:Fire()
end
-- When joining
Player.PlayerAdded:connect(CreateLeaderBoard)
Player.PlayerRemoving:connect(SaveData)
game:BindToClose(function()
while PLAYERS_LEFT > 0 do
BindableEvent.Event:Wait()
end
end)