#DataStore problem

1 messages · Page 1 of 1 (latest)

hollow apex
#

I have gone trough infinite number of videos and forums on DataStore but it just doesnt want to save, i have a tool that adds a value (Gems) when its activated for example Gems = Gems + 1, and i have done the whole thing with DataStore but it just doesnt work. (this time with code :))

#

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)

acoustic island
#

Print out the data just before you save it in the datastore, and just after you load it from the datastore.

hollow apex
#

so the print statement is before the saveData function and after it

acoustic island
hollow apex
#

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

acoustic island
#

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

hollow apex
#

Cookies.Value = PlayerData[1]

#

i think

acoustic island
hollow apex
#

like my tool script?

fresh wingBOT
#

studio** You are now Level 2! **studio

acoustic island
hollow apex
#
local tool = script.Parent
local player = game.Players.LocalPlayer

tool.Activated:Connect(function(plr)
    player.leaderstats.Cookies.Value = player.leaderstats.Cookies.Value + 1
end)
acoustic island
hollow apex
#

so should i add a remoteevent or just put it in a script

acoustic island
hollow apex
#

Thank you it saves now, have a good day.