Why is my leaderstats or other values in other folder keep changing the value back to 0

  1. **What do you want to achieve?

so i was making a settings system and leaderstats .

  1. **What is the issue?

idk why but all the values keep changing back to 0 even i write how much value i want in the script .

Here’s the script below


local DataStoreService = game:GetService("DataStoreService")
--Get myDataStore
local myDataStore = DataStoreService:GetDataStore("FifthOfUsDataStore")
--Satisfy Scripting
game.Players.PlayerAdded:Connect(function(player)
	--Leaderstats Folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	--Settings Folder
	local Options = Instance.new("Folder")
	Options.Name = "Settings"
	Options.Parent = player
	--Cash
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 100
	--ChosenCharacter
	local Char = Instance.new("IntValue")
	Char.Name = "Char"
	Char.Parent = leaderstats
	Char.Value = 3
	--Flashlight  1 = LMB 2 = RMB 3 = F
	local Flashlight = Instance.new("IntValue")
	Flashlight.Name = "Flashlight"
	Flashlight.Parent = Options
	Flashlight.Value = 2
	--Phone Control
	local PhoneControl = Instance.new("BoolValue")
	PhoneControl.Name = "PhoneControl"
	PhoneControl.Parent = Options
	--Shadows Softness
	local ShadowSoftness = Instance.new("BoolValue")
	ShadowSoftness.Name = "ShadowSoftness"
	ShadowSoftness.Parent = Options
	--Shadows
	local Shadows = Instance.new("BoolValue")
	Shadows.Name = "Shadows"
	Shadows.Parent = Options
	--FPS
	local ShowFPS = Instance.new("BoolValue")
	ShowFPS.Name = "ShowFPS"
	ShowFPS.Parent = Options
	--Ping
	local ShowPing = Instance.new("BoolValue")
	ShowPing.Name = "ShowPing"
	ShowPing.Parent = Options
	
	local data
	local success, errormessage = pcall(function()
		--Values Data
		data = myDataStore:GetAsync(player.UserId.."-Char")
		data = myDataStore:GetAsync(player.UserId.."-Flashlight")
		data = myDataStore:GetAsync(player.UserId.."-Cash")
		data = myDataStore:GetAsync(player.UserId.."-Flashlight")
		data = myDataStore:GetAsync(player.UserId.."-PhoneControl")
		data = myDataStore:GetAsync(player.UserId.."-ShadowSoftness")
		data = myDataStore:GetAsync(player.UserId.."-Shadows")
		data = myDataStore:GetAsync(player.UserId.."-ShowFPS")
		data = myDataStore:GetAsync(player.UserId.."-ShowPing")
	end)
	
	if success then 
		--Values Data
		Char.Value = data
		Flashlight.Value = data
		Cash.Value = data
		PhoneControl.Value = data
		ShadowSoftness.Value = data
		Shadows.Value = data
		ShowFPS.Value = data
		ShowPing.Value = data
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		--Values Data
		myDataStore:SetAsync(player.UserId.."-Char", player.leaderstats.Char.Value)
		myDataStore:SetAsync(player.UserId.."-Flashlight", player.Settings.Flashlight.Value)
		myDataStore:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
		myDataStore:SetAsync(player.UserId.."-PhoneControl", player.Settings.PhoneControl.Value)
		myDataStore:SetAsync(player.UserId.."-ShadowSoftness", player.Settings.ShadowSoftness.Value)
		myDataStore:SetAsync(player.UserId.."-Shadows", player.Settings.Shadows.Value)
		myDataStore:SetAsync(player.UserId.."-ShowFPS", player.Settings.ShowFPS.Value)
		myDataStore:SetAsync(player.UserId.."-ShowPing", player.Settings.ShowPing.Value)
	end)
	
	if success then 
		print("Player Data Successfully Saved !")
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)

--pls tell me why my leaderstats and other folder's value keep changing the value to 0 . Thanks !

please make this a block of code, I can’t read

im kinda new to this devforum . can u tell me how?

well, you already did, but not all

```lua
-- code
`` ` -- no space

that was due to spaces, they didn’t use what you showed

thanks for this info , if you didnt tell me , i still dont know how to do it lol . Thanks !

maybe it turns back to 0 because of this

then how im gonna save the data ??

do something like this:

IntValue.Value = Data1 or 20 -- set the "20" into your default value, and set the "data" variable into different variables like "data1", "data2" or else it will return in a same result

and to save data use :SetAsync() or either :UpdateAsync() to update the datasaved value

i try new script but still not working , the same problem .

is it cuz im testing in studio??


local DataStoreService = game:GetService("DataStoreService")
--Get myDataStore
local myDataStore = DataStoreService:GetDataStore("FifthOfUsDataStore")
--Satisfy Scripting
game.Players.PlayerAdded:Connect(function(player)
	--Leaderstats Folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	--Settings Folder
	local Options = Instance.new("Folder")
	Options.Name = "Settings"
	Options.Parent = player
	--Cash
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 100
	--ChosenCharacter
	local Char = Instance.new("IntValue")
	Char.Name = "Char"
	Char.Parent = leaderstats
	Char.Value = 3
	--Flashlight  1 = LMB 2 = RMB 3 = F
	local Flashlight = Instance.new("IntValue")
	Flashlight.Name = "Flashlight"
	Flashlight.Parent = Options
	Flashlight.Value = 2
	--Phone Control
	local PhoneControl = Instance.new("BoolValue")
	PhoneControl.Name = "PhoneControl"
	PhoneControl.Parent = Options
	--Shadows Softness
	local ShadowSoftness = Instance.new("BoolValue")
	ShadowSoftness.Name = "ShadowSoftness"
	ShadowSoftness.Parent = Options
	--Shadows
	local Shadows = Instance.new("BoolValue")
	Shadows.Name = "Shadows"
	Shadows.Parent = Options
	--FPS
	local ShowFPS = Instance.new("BoolValue")
	ShowFPS.Name = "ShowFPS"
	ShowFPS.Parent = Options
	--Ping
	local ShowPing = Instance.new("BoolValue")
	ShowPing.Name = "ShowPing"
	ShowPing.Parent = Options
	
	local data1
	--Equipped Character
	local success, errormessage = pcall(function()
		data1 = myDataStore:GetAsync(player.UserId.."-Char")
	end)
	
	local data2
	-- Flashlight
	local success, errormessage = pcall(function()
		data2 = myDataStore:GetAsync(player.UserId.."-Flashlight")
	end)
	
	local data3
	--Cash
	local success, errormessage = pcall(function()
		data3 = myDataStore:GetAsync(player.UserId.."-Cash")	
	end)
	
	local data4
	--Settings Saving
	local success, errormessage = pcall(function()
		data4 = myDataStore:GetAsync(player.UserId.."-PhoneControl")
		data4 = myDataStore:GetAsync(player.UserId.."-ShadowSoftness")
		data4 = myDataStore:GetAsync(player.UserId.."-Shadows")
		data4 = myDataStore:GetAsync(player.UserId.."-ShowFPS")
		data4 = myDataStore:GetAsync(player.UserId.."-ShowPing")
	end)
	
	if success then 
		--Values Data
		Char.Value = data1
		Flashlight.Value = data2
		Cash.Value = data3
		PhoneControl.Value = data4
		ShadowSoftness.Value = data4
		Shadows.Value = data4
		ShowFPS.Value = data4
		ShowPing.Value = data4
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		--Values Data
		myDataStore:SetAsync(player.UserId.."-Char", player.leaderstats.Char.Value)
		myDataStore:SetAsync(player.UserId.."-Flashlight", player.Settings.Flashlight.Value)
		myDataStore:SetAsync(player.UserId.."-Cash", player.leaderstats.Cash.Value)
		myDataStore:SetAsync(player.UserId.."-PhoneControl", player.Settings.PhoneControl.Value)
		myDataStore:SetAsync(player.UserId.."-ShadowSoftness", player.Settings.ShadowSoftness.Value)
		myDataStore:SetAsync(player.UserId.."-Shadows", player.Settings.Shadows.Value)
		myDataStore:SetAsync(player.UserId.."-ShowFPS", player.Settings.ShowFPS.Value)
		myDataStore:SetAsync(player.UserId.."-ShowPing", player.Settings.ShowPing.Value)
	end)
	
	if success then 
		print("Player Data Successfully Saved !")
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)

Instead of using GetAsync() and SetAsync() many times, just put those values ​​in a table and save the table.

can you make a example ?

im quite new to datastore , i dont know how to save a table

Here is an example of how to save it with a table

local data = {
Char = player.leaderstats.Char.Value,
Flashlight = player.Settings.Flashlight.Value,
Cash = player.leaderstats.Cash.Value,
PhoneControl = player.Settings.PhoneControl.Value,
ShadowSoftness = player.Settings.ShadowSoftness.Value,
Shadows = player.Settings.Shadows.Value,
ShowFPS = player.Settings.ShowFPS.Value,
ShowPing = player.Settings.ShowPing.Value
}

myDataStore:SetAsync(player.UserId, data)

And then you can get the data like this:

	local data
	local success, errormessage = pcall(function()
		--Values Data
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success and data then 
		--Values Data
		Char.Value = data.Char
		Flashlight.Value = data.Flashlight
		Cash.Value = data.Cash
		PhoneControl.Value = data.PhoneControl
		ShadowSoftness.Value = data.ShadowSoftness
		Shadows.Value = data.Shadows
		ShowFPS.Value = data.ShowFPS
		ShowPing.Value = data.ShowPing
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)

ok , i will see if it’s working or not . Thanks for the example btw .

You should try it on a roblox game client. I think the data is not saved in roblox studio.

You should also use DataModel | Roblox Creator Documentation to save the data before the game shutdown.

1 Like

there is a error “nil”


local DataStoreService = game:GetService("DataStoreService")
--Get myDataStore
local myDataStore = DataStoreService:GetDataStore("FifthOfUsDataStore")
--Satisfy Scripting
game.Players.PlayerAdded:Connect(function(player)
	--Leaderstats Folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	--Settings Folder
	local Options = Instance.new("Folder")
	Options.Name = "Settings"
	Options.Parent = player
	--Cash
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 100
	--ChosenCharacter
	local Char = Instance.new("IntValue")
	Char.Name = "Char"
	Char.Parent = leaderstats
	Char.Value = 3
	--Flashlight  1 = LMB 2 = RMB 3 = F
	local Flashlight = Instance.new("IntValue")
	Flashlight.Name = "Flashlight"
	Flashlight.Parent = Options
	Flashlight.Value = 2
	--Phone Control
	local PhoneControl = Instance.new("BoolValue")
	PhoneControl.Name = "PhoneControl"
	PhoneControl.Parent = Options
	--Shadows Softness
	local ShadowSoftness = Instance.new("BoolValue")
	ShadowSoftness.Name = "ShadowSoftness"
	ShadowSoftness.Parent = Options
	--Shadows
	local Shadows = Instance.new("BoolValue")
	Shadows.Name = "Shadows"
	Shadows.Parent = Options
	--FPS
	local ShowFPS = Instance.new("BoolValue")
	ShowFPS.Name = "ShowFPS"
	ShowFPS.Parent = Options
	--Ping
	local ShowPing = Instance.new("BoolValue")
	ShowPing.Name = "ShowPing"
	ShowPing.Parent = Options
	
	local data
	local success, errormessage = pcall(function()
		--Values Data
		data = myDataStore:GetAsync(player.UserId)
	end)

	if success and data then 
		--Values Data
		Char.Value = data.Char
		Flashlight.Value = data.Flashlight
		Cash.Value = data.Cash
		PhoneControl.Value = data.PhoneControl
		ShadowSoftness.Value = data.ShadowSoftness
		Shadows.Value = data.Shadows
		ShowFPS.Value = data.ShowFPS
		ShowPing.Value = data.ShowPing
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local success, errormessage = pcall(function()
		--Values Data
		local data = {
			Char = player.leaderstats.Char.Value,
			Flashlight = player.Settings.Flashlight.Value,
			Cash = player.leaderstats.Cash.Value,
			PhoneControl = player.Settings.PhoneControl.Value,
			ShadowSoftness = player.Settings.ShadowSoftness.Value,
			Shadows = player.Settings.Shadows.Value,
			ShowFPS = player.Settings.ShowFPS.Value,
			ShowPing = player.Settings.ShowPing.Value
		}

		myDataStore:SetAsync(player.UserId, data)
	end)
	
	if success then 
		print("Player Data Successfully Saved !")
	else
		print("There is an error , Caution !")
		warn(errormessage)
	end
end)
local DataStoreService = game:GetService("DataStoreService")
--Get myDataStore
local myDataStore = DataStoreService:GetDataStore("FifthOfUsDataStore")
--Satisfy Scripting
game.Players.PlayerAdded:Connect(function(player)
	--Leaderstats Folder
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	--Settings Folder
	local Options = Instance.new("Folder")
	Options.Name = "Settings"
	Options.Parent = player
	--Cash
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	Cash.Value = 100
	--ChosenCharacter
	local Char = Instance.new("IntValue")
	Char.Name = "Char"
	Char.Parent = leaderstats
	Char.Value = 3
	--Flashlight  1 = LMB 2 = RMB 3 = F
	local Flashlight = Instance.new("IntValue")
	Flashlight.Name = "Flashlight"
	Flashlight.Parent = Options
	Flashlight.Value = 2
	--Phone Control
	local PhoneControl = Instance.new("BoolValue")
	PhoneControl.Name = "PhoneControl"
	PhoneControl.Parent = Options
	--Shadows Softness
	local ShadowSoftness = Instance.new("BoolValue")
	ShadowSoftness.Name = "ShadowSoftness"
	ShadowSoftness.Parent = Options
	--Shadows
	local Shadows = Instance.new("BoolValue")
	Shadows.Name = "Shadows"
	Shadows.Parent = Options
	--FPS
	local ShowFPS = Instance.new("BoolValue")
	ShowFPS.Name = "ShowFPS"
	ShowFPS.Parent = Options
	--Ping
	local ShowPing = Instance.new("BoolValue")
	ShowPing.Name = "ShowPing"
	ShowPing.Parent = Options

	local datagot
	local success,Fail = pcall(function()
		datagot = myDataStore:GetAsync(player.UserId)
	end)
	if success then
		Flashlight.Value = datagot.Flash
		Char.Value = datagot.Char
		Cash.Value = datagot.Cash
		PhoneControl.Value = datagot.Phone
		ShadowSoftness.Value = datagot.ShadowSoft
		Shadows.Value = datagot.Shadow
		ShowFPS.Value = datagot.FPS
		ShowPing.Value = datagot.Ping
	end
end)


game.Players.PlayerRemoving:Connect(function(player)
	local success,errorMessage = pcall(function()
		myDataStore:SetAsync(player.UserId,{
			Flash = player.Settings.Flashlight.Value,
			Char = player.leaderstats.Char.Value,
			Cash = player.leaderstats.Cash.Value,
			Phone = player.Settings.PhoneControl.Value,
			ShadowSoft = player.Settings.ShadowSoftness.Value,
			Shadow = player.Settings.Shadows.Value,
			FPS = player.Settings.ShowFPS.Value,
			Ping = player.Settings.ShowPing.Value
		})
	end)
	if not success then
		warn("Could not Save Data, Data might be LOST! - " .. errorMessage)
	end
end)

local runService = game:GetService("RunService")

local PlayersService = game:GetService("Players")
local Players = PlayersService:GetPlayers()

game:BindToClose(function()
	if runService:IsStudio() then
		wait(1)
	else
		
		for _, localPlayer in pairs(Players) do
			local UserId = localPlayer
			local success,errormsg = pcall(function()
				myDataStore:SetAsync(UserId,{
					Flash = localPlayer.Settings.Flashlight.Value,
					Char = localPlayer.leaderstats.Char.Value,
					Cash = localPlayer.leaderstats.Cash.Value,
					Phone = localPlayer.Settings.PhoneControl.Value,
					ShadowSoft = localPlayer.Settings.ShadowSoftness.Value,
					Shadow = localPlayer.Settings.Shadows.Value,
					FPS = localPlayer.Settings.ShowFPS.Value,
					Ping = localPlayer.Settings.ShowPing.Value
				})
			end)
		end
		
	end
end)

This should work.

1 Like

on line 54 , is it suppose to be like this ?? cuz i received error

		Flashlight.Value = datagot.Flashlight
		Char.Value = datagot.Char
		Cash.Value = datagot.Cash
		PhoneControl.Value = datagot.PhoneControl
		ShadowSoftness.Value = datagot.ShadowSoftness
		Shadows.Value = datagot.Shadows
		ShowFPS.Value = datagot.ShowFPS
		ShowPing.Value = datagot.ShowPing
	end

I dont think so.
what error did you get?

oh there is no error now , maybe sometime it will cause an error . Is there a way to not make an error out ?