Data not loading

hey there, I have a script for saving a number value and it’s not saving help would be appreciated.

local DataStoreService = game:GetService("DataStoreService")
local SettingTest = DataStoreService:GetDataStore("SettingTest")

function OnPlayerAdded(player)
	local folder = Instance.new("Folder")
	folder.Name = "Settings"
	folder.Parent = player
	local Setting1 = Instance.new("NumberValue")
	Setting1.Name = "Test"
	Setting1.Parent = player.Settings
	Setting1.Value = 0
	local SettingDataLoaded = Instance.new("BoolValue", player)
	SettingDataLoaded.Name = "SettingDataLoaded"
	SettingDataLoaded.Value = false

	local data
	local attempts = 10

	repeat
		local success, err = pcall(function()
			data = SettingTest:GetAsync(player.UserId)
		end)
		if success then
			print("success")
			SettingDataLoaded.Value = true
			break
		else
			warn("Was problem with datastore! (Attempts left ", attempts, ")")
		end
		task.wait(1)
		attempts = attempts - 1
	until attempts <= 0

	if not SettingDataLoaded.Value then
		warn("Player " .. player.Name .. "'s data has not been loaded!")
		player:Kick("Couldn't load your data, try to rejoin later")
		return
	end

end

function OnPlayerRemoving(player)
	if player:FindFirstChild("SettingDataLoaded") and player.SettingDataLoaded.Value == true then
		local attempts = 10

		repeat
			local success, err = pcall(function()
				SettingTest:SetAsync(player.UserId, player.Settings:FindFirstChild("Test").Value)
			end)
			if success then
				print("Success!")
			else
				warn("Was problem with datastore! (Attempts left ", attempts, ")")
			end
			task.wait(1)
			attempts = attempts - 1
		until attempts <= 0
		if attempts <= 0 then
			warn("Player " .. player.Name .. "'s cash data has not been saved!")
		end
	end
end

game.Players.PlayerAdded:Connect(OnPlayerAdded)
game.Players.PlayerRemoving:Connect(OnPlayerRemoving)
2 Likes

Is there any output in console? Sometimes if you are testing in studio (“play solo”) the PlayerAdded event does not catch the first player joining which would mean your OnPlayerAdded function never fires, and then on exit your OnPlayerRemoving function does not enter the first conditional block.

2 Likes

There sometimes can be some bugs when saving data in Studio, have you tried this system in-game?
By the way, you could easyily check whitch part does not run when running many prints. If they do not run, you know something is wrong there.
Hope this helped you :slight_smile:

1 Like

I did try it in-game, it did not work

the console locks like this

I don’t think you define CashDataLoaded…

Ops I used CashDataLoaded for my leader stats system I fixed that and it’s still not working.

Try defining a value for Setting1

I tried defining a value for Setting1 and it’s still not working.

remove ‘break’ from the if success statement on player removing function

I tried removing break from the if success statement on player removing function and it’s still not working.

It’s working now thank you for the help