My DataStore doesn't work

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

Oops, I forgot to parent the leaderstats in that example code. insert it in between here:

leaderstats.Name = "leaderstats"
leaderstats.Parent = plr -- insert it in between these other lines of code
local cash = Instance.new("IntValue")

Here is the fixed code:

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"
    leaderstats.Parent = plr
    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

well it works as the same as @EHGTR1903

This part of the script is flawed, you’ll want to use my example code because it will work better.

Ok but now we have to save it (30 charrrrr)

My example code saves the data as well

That part is just to check if it’s loading the datastore or not. I already said that here

yeah it’s for this i said but now we need to save