DataStore not working

So you cant find the value of the player in the datastore plugin right?

It’s just trying to make a datastore to count when a payer touches part to give a point.

-- 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 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)
    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)

So basically this script works fine:

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']

		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)
	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)

Be sure you have turned on the API Service game settings on.

1 Like

Will test it tomorrow, I hope it works.

It works thanks, will give everyone some credit for the help.

1 Like