#DataStore problem
1 messages · Page 1 of 1 (latest)
local DataStore = game:GetService("DataStoreService")
local Store = DataStore:GetDataStore("1")
game.Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder")
Leaderstats.Parent = Player
Leaderstats.Name = "leaderstats"
local Cookies = Instance.new("IntValue")
Cookies.Parent = Leaderstats
Cookies.Value = 0
Cookies.Name = "Cookies"
local PlayerData
local Success, ErrorMsg = pcall(function()
local PlayerId = Player.UserId
PlayerData = Store:GetAsync(PlayerId)
end)
if Success then
if PlayerData then
Cookies.Value = PlayerData[1]
end
else
warn(ErrorMsg)
end
end)
local function saveData(Player)
local leaderstats = Player.leaderstats
local PlayerData = {leaderstats.Cookies.Value}
local Success, ErrorMsg = pcall(function()
Store:SetAsync(Player.UserId, PlayerData)
end)
if not Success then
warn(ErrorMsg)
end
end
game.Players.PlayerRemoving:Connect(function(Player)
saveData(Player)
end)
game:BindToClose(function()
for i, Player in pairs(game.Players:GetPlayers()) do
saveData(Player)
end
end)
Print out the data just before you save it in the datastore, and just after you load it from the datastore.
so the print statement is before the saveData function and after it
Just print out the variable you're giving to the SetAsync, and print out what you get back from GetAsync immediately.
ok so its printing out a table { [1] = 0 } when i join and after i stop the game it still is 0 even though i increased the leaderbord score using a tool
okay, so now we know it's not the saving, it's how you set the value.
Show me the code where the value should be increased
no, where the value should be changing, not being loaded.
like my tool script?
** You are now Level 2! **
mhm
local tool = script.Parent
local player = game.Players.LocalPlayer
tool.Activated:Connect(function(plr)
player.leaderstats.Cookies.Value = player.leaderstats.Cookies.Value + 1
end)
You're changing the value in a local script. The server, the one doing the saving, doesn't see that.
so should i add a remoteevent or just put it in a script
I'd say RemoteEvent.
Thank you it saves now, have a good day.