I need help with DataStore

Hello, I have a problem with saving IntValue.
When I went looking for instructions on how to use DataStore, I noticed that they all saving LeaderStats.
I’m still beginner, so the question may seem trivial, but how to save IntValue if
it is in workspace?
my script:

local ds = game.GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = workspace.Value
	
	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
	else
		local NumberForSaving = {save1.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {workspace.Value.Value})
end)
1 Like

You would use thingsaved = IntValue.Value. Can’t make a full fleged example right now.

1 Like

Thank you for help, but I didn’t quite understand what I need to do

Rename the value in workspace to something other than Value, The compiler might think .Value is a property of workspace.

Once you’ve did that, remove the else part of you’re code.

By the way, Why are you saving it in a table when there is no need?, Its 1 value not 2 or more. Remove the brackets on the :SetAsync part

1 Like

thank you for help, but my script still doesnt work…

My script now:

local ds = game.GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = workspace.Time

	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved
	else
		local NumberForSaving = {save1.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {workspace.Value.Value})
end)

also i have this error:
Expected ':' not '.' calling member function GetService

local ds = game.GetService("DataStoreService"):GetDataStore("SaveData")

Change this line to:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
2 Likes

I think you shouldnt parent the intvalue to workspace, you should parent it to the player.
Also i am wondering why there is a wait on the 3rd line

1 Like

Try this:
local DataStoreService = game:GetService(“DataStoreService”)
local dataStore = DataStoreService:GetDataStore(“MyDataStore”) – This can be changed to whatever you want

local function saveData(player) – The functions that saves data

local tableToSave = {
    player.leaderstats.Level.Value; -- First value from the table
    player.leaderstats.Coins.Value -- Second value from the table
}

local success, err = pcall(function()
	dataStore:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)

if success then -- If the data has been saved
	print("Data has been saved!")
else -- Else if the save failed
	print("Data hasn't been saved!")
	warn(err)		
end

end

game.Players.PlayerAdded:Connect(function(player) – When a player joins the game

– // Assigning player stats //
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

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

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

local data 
local success, err = pcall(function()

	data = dataStore:GetAsync(player.UserId) -- Get the data from the datastore
	
end)

if success then -- If there were no errors and player loaded the data
	
	Level.Value = data[1] -- Set the Level to the first value of the table (data)
	Coins.Value = data[2] -- Set the coins to the second value of the table (data)
	
else -- The player didn't load in the data, and probably is a new player
	print("The player has no data!") -- The default will be set to 0
end

end)

game.Players.PlayerRemoving:Connect(function(player) – When a player leaves
local success, err = pcall(function()
saveData(player) – Save the data
end)

if success then
    print("Data has been saved")
else
    print("Data has not been saved!")
end

end)

game:BindToClose(function() – When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do – Loop through all the players
local success, err = pcall(function()
saveData(player) – Save the data
end)

    if success then
        print("Data has been saved")
    else
        print("Data has not been saved!")
    end
end

end)

(Sorry I think it glitched but copy the whole thing into one script. Also put the script into ServerScriptService)

You can also toggle the leaderstats name :slight_smile:

1 Like

Typos

1. First typo

This should be UserId

2. Second typo

You don’t need to use square brackets

3. Third typo

You don’t need to use NumberForSaving. Because plrkey is already stored in DataStore with the player’s user ID.

4. Fourth Typo

It should be local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

My advice

  1. Use “pcall” (Protected Call) because while saving values into DataStore, players may lose their data. There may be some issues while saving or loading data. So pcall may be useful for you.

  2. You should store the IntValue in the player’s folder because that IntValue must be only valid for the player.

So here is the revised script;

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder") -- Creates a folder and parents it to the player
    leaderstats.Name = "leaderstats" -- Name
    leaderstats.Parent = plr -- Parent

    local stage = Instance.new("IntValue") -- Creates an IntValue and parents it to the leaderstats folder
    stage.Name = "Stage" -- Name of the IntValue
    stage.Parent = leaderstats -- Parent
    stage.Value = 0 -- If you want to give a value to the player, change this.


    local data
    local success, errormessage = pcall(function() -- This is the function which controls data, if it is saved or not.
	    data = ds:GetAsync("id_"..plr.UserId)
    end)

    if success then
	    stage.Value = data
    else
	    print("There was an error!")
	    warn(errormessage)
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local success, errormessage = pcall(function()
	    ds:SetAsync("id_"..plr.UserId ,plr.leaderstats.Stage.Value) -- Saves the data as the player's user ID.
    end)

    if success then
	    print("Player data is successfully saved!")
    else
	    print("There was an error!")
	    warn(errormessage)
    end
end)
1 Like

Thank you for your help! there are many solutions here but i chose the most detailed

1 Like