Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Data Is not being Saved via Data Store Service?

Basically, I got the print "Data was succesfuly Saved" But Data is not being saved, what am I doing wrong?

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


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

	local Points = Instance.new("IntValue")
    Points.Name = "Points"
    Points.Parent = leaderstats
    
local dataTable = {
	Francs = Francs.Value,
	Points = Points.Value
}
    
    
    local success, errormessage = pcall(function()
       playerData:GetAsync(player.UserId, dataTable) 
    end)
    
    if success then 
       print("Data was saved")
    else
        print("Error while getting your data")
        warn(errormessage)
    end
end)



-- Player Removing

game.Players.PlayerRemoving:Connect(function(player)
    
	local dataTable = {
	Francs = player.leaderstats.Francs.Value,
	Points = player.leaderstats.Points.Value
}

    local success, errormessage = pcall(function()
        playerData:SetAsync(player.UserId, dataTable)
    end)
    
    if success then
        print("Data successfully saved!")
    else
        print("There was an error while saving the data")
        warn(errormessage)
    end
    
end)

1 comment

0

I removed that part and still same result

1 answer

Heya, so in your current script, you aren't setting the data to the table that you are getting from the datastore.

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


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

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

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

    local data
    local success, errormessage = pcall(function()
    data =   playerData:GetAsync(player.UserId)
    end)

    if success then 
      Francs.Value = data[1]
      Points.Value = data[2]
    else
        print("Error while getting your data")
        warn(errormessage)
    end
end)



-- Player Removing

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

    local dataTable = {
    Francs = player.leaderstats.Francs.Value,
    Points = player.leaderstats.Points.Value
}

    local success, errormessage = pcall(function()
        playerData:SetAsync(player.UserId, dataTable)
    end)

    if success then
        print("Data successfully saved!")
    else
        print("There was an error while saving the data")
        warn(errormessage)
    end

end)

3 comments

0

It's still not working, when i give value in the game, i check it via Server Side, and I can see the values, but when I reconnect, it doesn't save

0

@EEnergy8 Do you have API Services enabled in studio?

0

Yes, i had a method to save data but, I can't use it to save more than 1 data

This discussion is preserved as it was. New answers, comments, and votes are closed.