Need help with datastore

so for the past few hours, I’ve been trying to make the datastore work, but the thing is, it already works, but, I wanted to add a new value named “Booster” and for some weird reason, EVERYTHING is being saved, except the Booster, I have no idea why this is happening but if you know what could be the problem, then please let me know! thank you.

script:
(ignore the other stuff)

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData5")


local function SavePlayerData(player)
	
	local success, errormsg = pcall(function()
	
		local SaveData = {}
		
		for i, stats in pairs(player.leaderstats:GetChildren()) do
			
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId, SaveData)
	end)
	
	if not success then 
		return errormsg
	end			
end	


Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = Stats

	local Chords = Instance.new("IntValue")
	Chords.Name = "Chords"
	Chords.Value = 0
	Chords.Parent = Stats

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems" 
	Gems.Value = 0
	Gems.Parent = Stats

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 0
    Level.Parent = Stats
	
	local experience = Instance.new("IntValue")
	experience.Name = "Total XP"
	experience.Value = 0
	experience.Parent = Stats
	
	local Booster = Instance.new("IntValue")
	Booster.Name = "Booster"
	Booster.Value = 0
	Booster.Parent = Stats
	

	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
		
		for i, stats in pairs(Stats:GetChildren()) do
			
			stats.Value = Data[stats.Name]
		end		
			
	else		
		print(player.Name .. " has no data.")			
	end
			
	
	local expToLevelUp
		
	local expForPreviousLevel = 0
	
	
	while wait() do
		
		local levelBar = player.PlayerGui:WaitForChild("LevelBar")	
		
		if Level.Value < 1 then 
			
			expToLevelUp = 100 
			
		else
			
			expToLevelUp = math.floor(Level.Value ^ 1.3) * 200 + math.floor(Level.Value ^ 4)
		end
		
		
		if experience.Value >= expToLevelUp then
			
			Level.Value = Level.Value + 1	
		end
		
		expForPreviousLevel = math.floor((Level.Value - 1) ^ 1.3) * 200 + math.floor((Level.Value - 1) ^ 4)
		
		
		local expDifference = expToLevelUp - expForPreviousLevel

		local expDifference2 = experience.Value - expForPreviousLevel
			
		
		levelBar.Bar:TweenSize(UDim2.new(levelBar.BarBackground.Size.X.Scale * (expDifference2 / expDifference), 0, levelBar.BarBackground.Size.Y.Scale, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.001)
		
		levelBar.Experience.Text = expDifference2 .. "/" .. expDifference
		
		levelBar.Level.Text = "Level: " .. Level.Value
		
		
		experience.Value = experience.Value + 1
	end			
end)


Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	
	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)
1 Like

It would be helpful if you provided some code xD

1 Like

ignore the other stuff in the script, focus on the leaderstats and datastore.

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local SaveDataStore = DataStoreService:GetDataStore("SaveData5")


local function SavePlayerData(player)
	
	local success, errormsg = pcall(function()
	
		local SaveData = {}
		
		for i, stats in pairs(player.leaderstats:GetChildren()) do
			
			SaveData[stats.Name] = stats.Value
		end	
		SaveDataStore:SetAsync(player.UserId, SaveData)
	end)
	
	if not success then 
		return errormsg
	end			
end	


Players.PlayerAdded:Connect(function(player)
	
	local Stats = Instance.new("Folder")
	Stats.Name = "leaderstats"
	Stats.Parent = player
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = Stats

	local Chords = Instance.new("IntValue")
	Chords.Name = "Chords"
	Chords.Value = 0
	Chords.Parent = Stats

	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems" 
	Gems.Value = 0
	Gems.Parent = Stats

	local Level = Instance.new("IntValue")
	Level.Name = "Level"
	Level.Value = 0
    Level.Parent = Stats
	
	local experience = Instance.new("IntValue")
	experience.Name = "Total XP"
	experience.Value = 0
	experience.Parent = Stats
	
	local Booster = Instance.new("IntValue")
	Booster.Name = "Booster"
	Booster.Value = 0
	Booster.Parent = Stats
	

	local Data = SaveDataStore:GetAsync(player.UserId)
	
	if Data then
		
		for i, stats in pairs(Stats:GetChildren()) do
			
			stats.Value = Data[stats.Name]
		end		
			
	else		
		print(player.Name .. " has no data.")			
	end
			
	
	local expToLevelUp
		
	local expForPreviousLevel = 0
	
	
	while wait() do
		
		local levelBar = player.PlayerGui:WaitForChild("LevelBar")	
		
		if Level.Value < 1 then 
			
			expToLevelUp = 100 
			
		else
			
			expToLevelUp = math.floor(Level.Value ^ 1.3) * 200 + math.floor(Level.Value ^ 4)
		end
		
		
		if experience.Value >= expToLevelUp then
			
			Level.Value = Level.Value + 1	
		end
		
		expForPreviousLevel = math.floor((Level.Value - 1) ^ 1.3) * 200 + math.floor((Level.Value - 1) ^ 4)
		
		
		local expDifference = expToLevelUp - expForPreviousLevel

		local expDifference2 = experience.Value - expForPreviousLevel
			
		
		levelBar.Bar:TweenSize(UDim2.new(levelBar.BarBackground.Size.X.Scale * (expDifference2 / expDifference), 0, levelBar.BarBackground.Size.Y.Scale, 0), Enum.EasingDirection.InOut, Enum.EasingStyle.Quint, 0.001)
		
		levelBar.Experience.Text = expDifference2 .. "/" .. expDifference
		
		levelBar.Level.Text = "Level: " .. Level.Value
		
		
		experience.Value = experience.Value + 1
	end			
end)


Players.PlayerRemoving:Connect(function(player)
	
	local errormsg = SavePlayerData(player)
	
	if errormsg then	
		warn(errormsg)		
	end	
end)

game:BindToClose(function()
	for i, player in pairs(Players:GetPlayers()) do	
		
		local errormsg = SavePlayerData(player)
		if errormsg then
			warn(errormsg)
		end			
	end
	wait(2)	
end)

hi, can you go into studio and test the game. Once you are in-game, click Players then click your player. Then, click leaderstats or whatever it is called and check if the ‘Booster’ value is there.

yep, i just checked, its in leaderstats

when you run this try adding print(stats.Name) and if you can, please let me know what the console prints

18:35:03.014 Rebirths - Server - LevelHandler:70
18:35:03.014 Chords - Server - LevelHandler:70
18:35:03.014 Gems - Server - LevelHandler:70
18:35:03.015 Level - Server - LevelHandler:70
18:35:03.015 Total XP - Server - LevelHandler:70
18:35:03.015 Booster - Server - LevelHandler:70

can you add a print on top of it

( print(SaveData) )

nothing was printed, thats weird

hold on. does it have anything to do with this? (this is the script for the upgrade boosters button)

local players = game:GetService("Players")
local localplayer = players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if localplayer.leaderstats.Chords.Value >= 1850 then

		localplayer.leaderstats.Booster.Value = localplayer.leaderstats.Booster.Value + 1
		end
end)
1 Like

That may be the problem - if this is a local script, changing the value here probably wont get back to the server, so I think if you check the value of Booster when you save, it will likely still be 0

Looks like the other changes are happening in server side code which is why they are saving ok.

You’d need to fire off a remote event to update it on the server, it should then be alright as the rest of the saving code all looks good.

1 Like

that makes sense actually, it might work, i’ll try it.

1 Like

it worked, thank you so much! i really appreciate it dude

1 Like