EDIT - Datastore and default values

EDIT ---- SEE BOTTOM FOR UPDATE -----

Hey there, currently not getting any errors and does not start a player off with the default values given for gold/level… not sure what I am doing wrong. Tried to comment everything so it was more understandable

-- GET DATA STORE SERVICE--
local DataStoreService = game:GetService("DataStoreService")

--------------------------------------------

--DATA STORES--
local GoldStore = DataStoreService:GetDataStore("PlayerGold")
local LevelStore = DataStoreService:GetDataStore("PlayerLevel")

--------------------------------------------

-- ON PLAYER ADDED --
game.Players.PlayerAdded:Connect(function(player)
	
	wait()
	
	-- LEAVE THE STAT VARIABLES AS GLOBAL (NON LOCAL VARIABLES) SO WHOLE SCRIPT CAN USE --
	
	-- LEADERSTATS --
	
	leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	-- ADD LEADERSTATS BELOW --
	

	GoldCoins = Instance.new("IntValue")
	GoldCoins.Name = "GoldCoins"
	GoldCoins.Parent = leaderstats
	GoldCoins.Value = 200
	
	Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats

	
	-- PLAYER STATS --
	
	playerStats = Instance.new("Folder")
	playerStats.Name = "playerStats"
	playerStats.Parent = player
	
	-- ADD PLAYER STATS BELOW --
	
	
	Experience = Instance.new("IntValue")
	Experience.Name = "Experience"
	Experience.Value = 0
	Experience.Parent = playerStats
	
	maxExperience = Instance.new("IntValue")
	maxExperience.Name = "maxExperience"
	maxExperience.Value = 1100
	maxExperience.Parent = playerStats
	
	
	
	--------------------------------------------
	
	
	-- RESOURCES --
	
	resourceStats = Instance.new("Folder")
	resourceStats.Name = "resourceStats"
	resourceStats.Parent = player
	
	-- ADD RESOURCES BELOW --
	
	
	
	
	
	--------------------------------------------
	
	-- ITEMS --
	itemStats = Instance.new("Folder")
	itemStats.Name = "itemStats"
	itemStats.Parent = player
	
	-- ADD ITEMS BELOW --
	
	
	
	--------------------------------------------
	
	
	--DATA VARIABLES SET AS NIL VALUE FOR NOW, GET UPDATED BELOW --
	local GoldCoinsData
	local LevelData
	local ExpData
	
	
	-------------------------------------------
	local success, errormessage = pcall(function() 
		GoldCoinsData = GoldStore:GetAsync(player.UserId.."-GoldCoins")
		LevelData = LevelStore:GetAsync(player.UserId.."-Level")
	end)
	
	
	if success then -- if success then sets a the value to DATA variable
		GoldCoins.Value = GoldCoinsData
		Level.Value = LevelData
	else
		warn(errormessage) -- if fails then gives error
	end
end)

-- ON PLAYER REMOVE --
game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		GoldStore:SetAsync(player.UserId.."-GoldCoins", player.leaderstats.GoldCoins.Value)
		LevelStore:SetAsync(player.UserId.."-Level", player.leaderstats.Level.Value)
	end)

	if success then -- if success then the data gets saved via SetAsync above
		print("DATA SAVED")
	else
		warn(errormessage) -- if fails then gives error
	end
end)

-- LOOP ON PLAYER ADDED TO SAVE DATA EVERY 120 SECONDS)
game.Players.PlayerAdded:Connect(function(player)

	while true do

		wait(300) -- SAVE INTERVAL (SAFTEY IN CASE SERVER CRASHES, ETC)

		local success, errormessage = pcall(function()
			GoldStore:SetAsync(player.UserId.."-GoldCoins", player.leaderstats.GoldCoins.Value)
			LevelStore:SetAsync(player.UserId.."-Level", player.leaderstats.Level.Value)
		end)

		if success then -- if success then the data gets saved via SetAsync above
			print("DATA SAVED")
		else
			warn(errormessage) -- if fails then gives error
		end
	end

end)



So I was able to get the player added to have the default to 1 by doing this:

if success then -- if success then sets a the value to DATA variable
		GoldCoins.Value = GoldCoinsData
		if LevelData == 0 then
			Level.Value = 1
		elseif
			LevelData >= 1 then
			Level.Value = LevelData
		end

however when I actually change the value in the console log in roblox client game, it does not save the data…

if success then -- if success then sets a the value to DATA variable
		GoldCoins.Value = GoldCoinsData
		if LevelData == 0 then
			Level.Value = 1
		elseif
			LevelData >= 1 then
			Level.Value = LevelData
		end

if it is changing the default values even though they don’t have data, add some if statements like so:

if GoldCoinsData then
    GoldCoins.Value = GoldCoinsData
end
if LevelData then
    Level.Value = LevelData
end

as a noob here and still learning logic… I’m unsure of where that if statement would fall in line…

also if it helps at all - it defaults the values to 0 for new players even though the values are set forth in the stats portion

the if statements would replace the lines inside of where you change their stats. The way the if statements work is to check whether they are nil or not.

GoldCoins = Instance.new("IntValue")
	GoldCoins.Name = "GoldCoins"
	GoldCoins.Parent = leaderstats
	if GoldCoinsData then
		GoldCoins.Value = GoldCoinsData
	end

like this?

No, here is the part I’m talking about:

Sorry I waited so long to respond.

1 Like

it worked! Thank you for your help! Just been bashing my head against the wall trying to learn data stores lol.