#DataStore not storing :(

1 messages · Page 1 of 1 (latest)

shy cipher
#
local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("Stats")

game.Players.PlayerAdded:Connect(function(plr)
    local brainAmount = plr.PlayerGui:WaitForChild("BrainAmountGUI").IntValue
    
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local money = Instance.new("NumberValue")
    money.Name = "Money"
    money.Parent = leaderstats
    
    local moneyValue = data:GetAsync(plr.UserId .. "-Money")
    local brainValue = data:GetAsync(plr.UserId .. "-Brain")
    
    if moneyValue ~= nil then
        plr.leaderstats.Money.Value = moneyValue
    end
    
    if brainValue ~= nil then
        brainAmount.Value = brainValue
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local brainAmount = plr.PlayerGui:WaitForChild("BrainAmountGUI").IntValue
    
    local moneySuccess, moneyErr = pcall(function()
        data:SetAsync(plr.UserId .. "-Money", plr.leaderstats.Money.Value)
    end)
    
    if moneyErr then
        print("Money Error")
    end
    
    local brainSuccess, brainErr = pcall(function()
        data:SetAsync(plr.UserId .. "-Brain", brainAmount.Value)
    end)
    
    if brainErr then
        print("Brain Error")
    end
end)

Script inside ServerScriptStorage

short mortar
#

Have you turned on api services?

feral current
#

local ds = game:GetService("DataStoreService")
local data = ds:GetDataStore("Stats")

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

local money = Instance.new("NumberValue")
money.Name = "Money"
money.Parent = leaderstats

-- Create a BrainAmount IntValue under the player (not in PlayerGui)
local brainAmount = Instance.new("IntValue")
brainAmount.Name = "BrainAmount"
brainAmount.Parent = plr

-- Load Data
local success, result = pcall(function()
    return {
        Money = data:GetAsync(plr.UserId .. "-Money"),
        Brain = data:GetAsync(plr.UserId .. "-Brain")
    }
end)

if success then
    if result.Money ~= nil then
        money.Value = result.Money
    end
    if result.Brain ~= nil then
        brainAmount.Value = result.Brain
    end
else
    warn("Failed to load data for", plr.Name)
end

end)

game.Players.PlayerRemoving:Connect(function(plr)
local money = plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("Money")
local brainAmount = plr:FindFirstChild("BrainAmount")

if money and brainAmount then
    local success, err = pcall(function()
        data:SetAsync(plr.UserId .. "-Money", money.Value)
        data:SetAsync(plr.UserId .. "-Brain", brainAmount.Value)
    end)

    if not success then
        warn("Failed to save data for", plr.Name, ":", err)
    end
end

end)

shy cipher
shy cipher
livid badger
shy cipher
#

I have