DataStore not working

I am making a datastore that will save the players points and cash but it either says it can’t save or just nothing. I have tried just doing it for one value and the same problems happen. The player will get points by touching a part and cash by touching another part.

-- DataStore 
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerDataStore")



local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local cash = Instance.new("IntValue") --Sets up value for leaderstats

	cash.Name = "Cash"

	cash.Parent = leaderstats



	local points = Instance.new("IntValue") --Sets up value for leaderstats

	points.Name = "Points"

	points.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		cash.Value = data['Cash']

		points.Value = data['Points']

	else

		-- Data store is working, but no current data for this player

		cash.Value = 0

		points.Value = 0

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)


-- Part script
local part = script.Parent

local canGet = true



local function onTouch(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')

	if humanoid then

		local player = game.Players:FindFirstChild(otherPart.Parent.Name)

		if player and canGet then

			canGet = false

			player.leaderstats.Points.Value = player.leaderstats.Points.Value + 1 

			wait(60)

			canGet = true

		end

	end

end



part.Touched:Connect(onTouch)

image image

Use DataStore2 instead.

local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		cash.Value = data['Cash']

		points.Value = data['Points']

	else

Big mistake, because you have to put it in a pcall to make sure it works.

local data
local success, errormessage = pcall(function()
   data =  playerData:GetAsync(playerUserId)
end)
	if success then

		cash.Value = data['Cash']

		points.Value = data['Points']

	else
1 Like

Side note, but you should also check if the data is equal to a saved Number so that it doesn’t error as a nil value:

local data
local success, errormessage = pcall(function()
   data =  playerData:GetAsync(playerUserId)
end)
	if success and data then

		cash.Value = data['Cash']

		points.Value = data['Points']

	else
2 Likes

Just so you know, DataStores will not work in studio. Make sure you test in the actual game! (I’m not sure why it doesn’t)

1 Like

They do work in studio? Are you slow lol? Its because the server sometimes closes before the player leaves causing it not to save!. Thats why you should use game:BindToClose(function() end)!

2 Likes

I know, however, I did not see :BindToClose() in his script and I did not want to complicate things! You are correct though.

1 Like

Does this go on line 78 in the original code which is:

local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)
-- DataStore 
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerDataStore")



local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local cash = Instance.new("IntValue") --Sets up value for leaderstats

	cash.Name = "Cash"

	cash.Parent = leaderstats



	local points = Instance.new("IntValue") --Sets up value for leaderstats

	points.Name = "Points"

	points.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data
local success, errormessage = pcall(function()
   data =  playerData:GetAsync(playerUserId)
end)
	if success and data then

		cash.Value = data['Cash']

		points.Value = data['Points']

	else

		-- Data store is working, but no current data for this player

		cash.Value = 0

		points.Value = 0

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)

	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

I got no error but it didn’t work. I tried putting game:BindToClose(function() end) at the end but I don’t know if that is the correct place for it.

So it doesn’t save the data?

Yeah it dosen’t save, no error or warning.

So is the create_table function creating a dictionary?

{
['Cash'] = {0};
['Points'] = {0}
}

Try doing this to make sure it makes a dictionary

local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end
print(create_table(player))

Put it inside of the onPlayerJoin function

There is a leaderstats folder in player.Master1794 if thats what you mean.

Yeah, but when you run the create_table() function it needs to create a dictionary.

This came up on the output 19:00:39.993 :arrow_forward: {…} - Server - DataStore:65

-- DataStore 
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerDataStore")



local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local cash = Instance.new("IntValue") --Sets up value for leaderstats

	cash.Name = "Cash"

	cash.Parent = leaderstats



	local points = Instance.new("IntValue") --Sets up value for leaderstats

	points.Name = "Points"

	points.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data
local success, errormessage = pcall(function()
   data =  playerData:GetAsync(playerUserId)
end)
	if success and data then

		cash.Value = tonumber(data['Cash'][1])

		points.Value = tonumber(data['Points'][1])

	else

		-- Data store is working, but no current data for this player

		cash.Value = 0

		points.Value = 0

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)
    print(player_stats)
	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

Got this: 19:03:02.285 ServerScriptService.DataStore:42: attempt to index number with number - Server - DataStore:42
19:03:02.285 Stack Begin - Studio
19:03:02.285 Script ‘ServerScriptService.DataStore’, Line 42 - function onPlayerJoin - Studio - DataStore:42
19:03:02.286 Stack End - Studio

Anymore things I will test tomorrow.

-- DataStore 
local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerDataStore")



local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local cash = Instance.new("IntValue") --Sets up value for leaderstats

	cash.Name = "Cash"

	cash.Parent = leaderstats



	local points = Instance.new("IntValue") --Sets up value for leaderstats

	points.Name = "Points"

	points.Parent = leaderstats



	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data
local success, errormessage = pcall(function()
   data =  playerData:GetAsync(playerUserId)
end)
	if success then

		cash.Value = data['Cash'][1]

		points.Value =data['Points'][1]

	else

		-- Data store is working, but no current data for this player

		cash.Value = 0

		points.Value = 0

	end

end



local function create_table(player)

	local player_stats = {}

	for _, stat in pairs(player.leaderstats:GetChildren()) do

		player_stats[stat.Name] = stat.Value

	end

	return player_stats

end



local function onPlayerExit(player)  --Runs when players exit



	local player_stats = create_table(player)
    print(player_stats)
	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)