#DataStore dont work

48 messages · Page 1 of 1 (latest)

thin sentinel
#

Hi, i dont know a thing about studio, but im doing a game -_-

i was searching some videos on yt and trying a LOT of diferent strats but none of them worked

#

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

game.Players.PlayerAdded:Connect(function(Player)
-- Create a NumberValue to store the Gold
local goldValue = Instance.new("NumberValue")
goldValue.Name = "Gold"
goldValue.Parent = Player

-- Retrieve value from DataStore
local success, playerData = pcall(function()
    return GoldStore:GetAsync(tostring(Player.UserId))
end)

if success then
    if playerData ~= nil then
        goldValue.Value = playerData
        print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
    else
        goldValue.Value = 225
        print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
    end
else
    warn("Failed to get data for " .. Player.Name .. ": " .. playerData)
    goldValue.Value = 225
end

-- Connect value change to save in DataStore
goldValue.Changed:Connect(function()
    local success, errorMessage = pcall(function()
        GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
    end)
    
    if not success then
        warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
    else
        print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
    end
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local goldValue = Player:FindFirstChild("Gold")
if goldValue then
local success, errorMessage = pcall(function()
GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
end)

    if not success then
        warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
    else
        print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
    end
end

end)

#

When i enter the game it says that i have 225 gold, but in my last play i was at a diferent one, like 125

dry ivy
#

Is this a game that you want to make?

thin sentinel
dry ivy
#

i recommend to learn everything first and to practice certain aspects

#

untill you know some things to implement into your game

#

i have a game planned but im learning some stuff about Lua to get myself ready to make a game i want to make

#

but also make some games first to understand in the long run

thin sentinel
#

ty, but can u help me with the code?

dry ivy
#
local DataStoreService = game:GetService("DataStoreService")
local GoldStore = DataStoreService:GetDataStore("GoldStore")

game.Players.PlayerAdded:Connect(function(Player)
    -- Create a NumberValue to store the Gold
    local goldValue = Instance.new("NumberValue")
    goldValue.Name = "Gold"
    goldValue.Parent = Player

    -- Retrieve value from DataStore
    local success, playerData = pcall(function()
        return GoldStore:GetAsync(tostring(Player.UserId))
    end)

    if success then
        if playerData ~= nil then
            goldValue.Value = playerData
            print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
        else
            goldValue.Value = 225
            print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
        end
    else
        warn("Failed to get data for " .. Player.Name .. ": " .. playerData)
        goldValue.Value = 225
    end

    -- Connect value change to save in DataStore
    goldValue.Changed:Connect(function()
        local success, errorMessage = pcall(function()
            GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
        end)

        if not success then
            warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
        else
            print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
        end
    end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
    local goldValue = Player:FindFirstChild("Gold")
    if goldValue then
        local success, errorMessage = pcall(function()
            GoldStore:SetAsync(tostring(Player.UserId), goldValue.Value)
        end)

        if not success then
            warn("Failed to save data for " .. Player.Name .. ": " .. errorMessage)
        else
            print("Gold value saved in DataStore for " .. Player.Name .. ": " .. goldValue.Value)
        end
    end
end)

#

what's the problem?

thin sentinel
#

the game keeps saying that i have 225 gold

sharp skiffBOT
#

studio** You are now Level 1! **studio

thin sentinel
#

the data store dont update

dry ivy
thin sentinel
#

the starter value is 225

#

but when i change this, leave and come back, it goes back to 225

dry ivy
#

then it saved the data, no?

#

did you retype the value inside of the client?

thin sentinel
dry ivy
#

then it worked

thin sentinel
#

but it was suposed to save with another gold value

dry ivy
#

can you video of the situation?

dry ivy
thin sentinel
#

local goldValue = Instance.new("NumberValue")
goldValue.Name = "Gold"
goldValue.Parent = Player

thin sentinel
dry ivy
#

so is the gold value where you placed the "225" value?

thin sentinel
#

but when i spend the gold, the data stil saves 225

#

but it is suposed to save the actual gold

dry ivy
#

you saved the data to set it as 225


    if success then
        if playerData ~= nil then
            goldValue.Value = playerData
            print("Gold value retrieved from DataStore for " .. Player.Name .. ": " .. playerData)
        else
            goldValue.Value = 225
            print("No data found in DataStore. Default value of 225 used for " .. Player.Name)
        end
dry ivy
#

then after you spent it, it's meant to subtract it right?

#

if not then i don't possibly know

thin sentinel
#

and save the value 225 - spent value

dry ivy
# thin sentinel yes

then whatever script you have that does this, make sure your subtracting the value after someone buy's something

thin sentinel
sharp skiffBOT
#

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

thin sentinel
#

Started test

Gold value retrieved from DataStore for batata_152: 225

Gold value : 225

Spent gold

Gold Value : 125

Ended test

Gold value saved in DataStore for batata_152: 225

#

u here? @dry ivy

thin sentinel
#

nice

dry ivy
#

I feel like you should figure out why the data isn’t saving

#

Maybe break the code down to understand why that’s happening