Table not saving

local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')


game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	local WaterData, RebirthsData
	local playerData = {
		player:WaitForChild('leaderstats').Water.Value;
		player:WaitForChild('leaderstats').Rebirths.Value
	}
	
	local succ, err = pcall(function()
		WaterData = Datastore:GetAsync(playerUserId)
		RebirthsData = Datastore:GetAsync(playerUserId)
	end)
	
	if not succ then
		warn(err)
	else
		playerData[1] = WaterData
		playerData[2] = RebirthsData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	local playerData = {
		player:WaitForChild('leaderstats').Water.Value;
		player:WaitForChild('leaderstats').Rebirths.Value
	}
	local succ, err = pcall(function()
		Datastore:SetAsync(playerUserId, playerData[1])
		Datastore:SetAsync(playerUserId, playerData[2])
	end)
	
	if succ then
		print('saved!')
	else
		warn(err)
	end
end)

usng tables to save certain values from the player. it says data is saved but it is not saving. what is going on

2 Likes

Not sure if this would be the case, but you’re saving individual values of data…? Can’t you just save them all inside a table rather than just calling SetAsync/GetAsync twice?

local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')


game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId

	local Data
	local playerData = {
		player:WaitForChild('leaderstats').Water.Value;
		player:WaitForChild('leaderstats').Rebirths.Value
	}
	
	local succ, err = pcall(function()
		Data = Datastore:GetAsync(playerUserId)
	end)
	
	if not succ then
		warn(err)
	else
		playerData[1] = Data[1]
		playerData[2] = Data[2]
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	local playerData = {
		player:WaitForChild('leaderstats').Water.Value;
		player:WaitForChild('leaderstats').Rebirths.Value
	}
	local succ, err = pcall(function()
		Datastore:SetAsync(playerUserId, playerData)
	end)
	
	if succ then
		print('saved!')
	else
		warn(err)
	end
end)

Error occurred and it printed tried to print attempt to index nil wth a number. this occurs on line 21

1 Like

Hold on. Is that semicolon allowed to be there? Shouldn’t it be a comma?

I think you are allowed to use both semi and comma

I guess this is because the table is nil. After you quit the game, it saves. This means if you were new to the game, it couldn’t be found. So make it

if not succ then
    warn(err)
else
    if playerData ~= nil then
        playerData[1] = Data[1]
        playerData[2] = Data[2]
    else
        print("Player is new to the game.")
    end
end
1 Like

Hmm maybe try printing the tables Data and playerData separately to see which table isn’t working.

The data isn’t saving but it is printing saved?

yes that is exactly what is going on

tried and it did not save. :(((

I will try to figure it out im in studio

Try this and also make sure you are changing the value on the server not on the client


local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')


game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId

	local Data
	
	--local playerData = {
	--	player:WaitForChild('leaderstats').Water.Value;
	--	player:WaitForChild('leaderstats').Rebirths.Value
	--}
	

	local succ, err = pcall(function()
		Data = Datastore:GetAsync(playerUserId)
	end)
	
	
	if not succ then
		warn(err)
	else
		player:WaitForChild('leaderstats').Water.Value = Data[1]
		player:WaitForChild('leaderstats').Rebirths.Value = Data[2]
		print("Data loaded")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	
	local playerData = {
		player:WaitForChild('leaderstats').Water.Value;
		player:WaitForChild('leaderstats').Rebirths.Value
	}
	
	local succ, err = pcall(function()
		Datastore:SetAsync(playerUserId, playerData)
	end)

	if succ then
		print('saved!')
	else
		warn(err)
	end
end)

Then it would be better to use the BindToClose function because there is a very small chance of being saved in datastores. PlayerRemoving event won’t work consistently when the server shuts down.

In datastores you use BindToClose if the server shutdown or a player has crashed? I never used bindtoclose

The problem was that you was changing the value in the table not the actual player’s value

BindToClose is used when the server is going to shut down. It is sometimes used to undertake some of the actions that can’t be used when the game is going to be closed. But not players when they crash.

1 Like

Does this script work?

local DSS = game:GetService('DataStoreService')
local Datastore = DSS:GetDataStore('TapsDatastore')

game.Players.PlayerAdded:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	local WaterData, RebirthsData
	
	local Water = player:WaitForChild('leaderstats').Water
	local Rebirths = player:WaitForChild('leaderstats').Rebirths

	local succ, err = pcall(function()
		Data = Datastore:GetAsync(playerUserId)
	end)

	if succ then
		if Data ~= nil then
			Water.Value = Data.Water
			Rebirths.Value = Data.Rebirths
		else
			Water.Value = 0 -- or some other default value
			Rebirths.Value = 0 -- or some other default value
		end
	else
		warn(err)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = 'Player_' .. player.UserId
	local playerData = {
		Water = player.leaderstats.Water.Value;
		Rebirths = player.leaderstats.Rebirths.Value;
	}
	local succ, err = pcall(function()
		Datastore:SetAsync(playerUserId, playerData)
	end)
	if succ then
		print('saved!')
	else
		warn(err)
	end
end)
1 Like