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

DataStore is not storing values?

I'm trying to store the player's level and xp for my RPG in DataStorage but something beyond my comprehension is happening

game.Players.PlayerAdded:connect(function(player)
	repeat wait() until player.Character
	--#load#--
	local folder=Instance.new("Folder",player) folder.Name="Storage"
	local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
	local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
	--==--
	
	--#datastore#--
	local DataStore=game:getService("DataStoreService"):GetDataStore("StatStore")
	local key=("player-"..player.userId)
	local savestats=DataStore:GetAsync(key)
	if savestats then
		print("Found "..player.Name.."'s save file! Loading...")
		level.Value=savestats[1]
		xp.Value=savestats[2]
		print("Stats successfuly loaded!")
	else
		print("Making a new save for "..player.Name)
		local statstosave={level.Value,xp.Value}
		DataStore:GetAsync(key,statstosave)
		print("Made "..statstosave[#statstosave].." things to save in DataStorage for "..player.Name)
	end
	--==--

i know, i know, i dont use spacing in my coding- sorry about that. anyway, it says: >"Making a new save for Qorm" then >"Made 0 new things to save in DataStorage for Qorm"

help would be appreciated!!

2 answers

✓ Accepted answer

I would recommend using "Data persistence" instead.

Your first part for the Folder and Values is good. But saving it is different.

game.Players.PlayerAdded:connect(function(player)
     repeat wait() until player.Character
    local folder=Instance.new("Folder",player) folder.Name="Storage"
    local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
    local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
	xp.Value = player:LoadNumber("xp")
	level.Value = player:LoadNumber("level")
end)

game.Players.PlayerRemoving:connect(function(player)
    if player:WaitForDataReady() then
        player:SaveNumber("xp", ***Where ever you find the XP***)
	player:SaveNumber("level", ***Where ever you find the Level***)
    end   
end)

Source: Click

2 comments

0

Data persistence isn't reliable -.-

0

It works for me.

game.Players.PlayerAdded:connect(function(player)
    --player.CharacterAdded function instead but it isnt necessary in this case
    --#load#--
    local folder=Instance.new("Folder",player) folder.Name="Storage"
    local level=Instance.new("IntValue",folder) level.Name="level" level.Value=1
    local xp=Instance.new("IntValue",folder) xp.Name="xp" xp.Value=0
    --==--

    --#datastore#--
    local DataStore=game:getService("DataStoreService"):GetDataStore("StatStore")
    local key=("player-"..player.userId)
    local savestats=DataStore:GetAsync(key)
    if savestats then
        print("Found "..player.Name.."'s save file! Loading...")
        level.Value=savestats[1]
        xp.Value=savestats[2]
        print("Stats successfuly loaded!")
    else
        print("Making a new save for "..player.Name)
        local statstosave={level.Value,xp.Value}
        DataStore:SetAsync(key,statstosave) --SetAsync, not GetAsync
        print("Made "..#statstosave.." things to save in DataStorage for "..player.Name)
    end
    --==--

Good to see you're learning datastores!

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