My DataStore doesn't work

i made a script for datastore that for the first 2 days it worked but now it doesn’t work idk if i changed something by mistake i tried to write again to change the parent but nothing
this is the script:

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats

    local data
    local success, errormessage = pcall(function()
        data = myDataStore:GetAsync(player.UserId.."-cash")
    end)

    if success then
        cash.Value = data
    else
        print("player has no data")
        warn(errormessage) 
    end
end)

game.Players.PlayerRemoving:Connect(function(player)

    local success, errormessage = pcall(function()
	    myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
    end)

    if success then
	    print("successsfully saved data")
    else
	    print("data save failed")
	    warn(errormessage)
    end
end)
1 Like

If u want to know when i test it print successfully saved data but it doesn’t save

Are there any errors? and Are sure that is doesn’t save? maybe it doesn’t load it. (30 charrrr)

Change “myDataStore”
mataStoreService:GetDataStore(“myDataStore”)
to something else and try again.

Try replacing it with this:

if success then
    if data then
        cash.Value = data
    else
        print("New player, no data")
    end
else
    warn(errormessage)
end
1 Like

There is no error and i don’t know if it doesn’t save or it doesn’t load

1 Like

Try this make a new Variable called DefaultCash and if the loading fails make the players cash Default Cash

local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local DefaultCash = 100

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats

local data
local success, errormessage = pcall(function()
    data = myDataStore:GetAsync(player.UserId.."-cash")
end)

if success then
    print(data)
    cash.Value = DefaulthCash
    print("Cash Has Been Loaded!")
else
    
    print("player has no data")
    warn(errormessage) 
end
end)

the script doesn’t work (30 char)

Use datastore2
30characterssss

but everytime a user join he will have 100 cash also if it have 0 or 200

well i’m not expert on scripting so i don’t how to use datastore2

Doesn’t work (30 charrrrrrrrrrrrrr)

dude datastore2 is so much easier and just better

that’s just to see if the loading works.

@biicciioo Try this (make sure it’s a ServerScript inside of ServerScriptService):

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")
local defaultCash = 50 -- change amount to your liking

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats

    local data
    local success, message = pcall(function()
        data = dataStore:GetAsync(plr.UserId.."-cash")
    end
    if success then
        if data then
            cash.Value = data
        else
            print(plr.Name.." is a new player")
            cash.Value = defaultCash
        end
    else
        warn(message)
    end
end

game.Players.PlayerRemoving:Connect(function(plr)
    local data = plr.leaderstats.Cash.Value
    local success, message = pcall(function()
        dataStore:SetAsync(plr.UserId.."-cash", data)
    end
    if success then
        print("data for "..plr.Name.." saved")
    else
        warn(message)
    end
end

Try this and get the DataStore2 Module and put it in ServerScriptService
@biicciioo

local DataStore2 = require(ServerScriptService.DataStore2)
DataStore2.Combine("DATA", "cash")

game.Players.PlayerAdded:Connect(function(player)
   local cashStore = DataStore2("cash", player)

   local leaderstats = Instance.new("Folder")
   leaderstats.Name = "leaderstats"
   leaderstats.Parent = player

   local Cash = Instance.new("IntValue")
   Cash.Name = "Cash"
   Cash.Value = 50 or nil;
   Cash.Parent = leaderstats

  cashStore:OnUpdate(function(newCash)
      Cash.Value = newCash
  end
end

Are you Testing it in Studio?
If so go to game Settings → security → and enable Studio Acces To API
and make sure your game is published.


GameSeetingSecurity

it worked (30 charrrrrrrrrrrrrrr)

1 Like

i knew this 30 charrrrrrrrrrrrrrrrrrrrrrrrrrr

if i do this the leaderstats won’t work