Data store won't work, and numbers only show up changing for one player!

I’ve faced this issue a few days ago, but yes it happened again. I tried everything and as the title says, it is still the same. I don’t know what’s causing it, I need help! Here’s the script:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	leaderstats.Archivable = true
	leaderstats.Parent = plr
	local CatfoodValue = Instance.new("IntValue")
	CatfoodValue.Name = "CatFood"
	CatfoodValue.Archivable = true
	CatfoodValue.Parent = leaderstats
	local XPValue = Instance.new("IntValue")
	XPValue.Name = "XP"
	XPValue.Archivable = true
	XPValue.Parent = leaderstats
	local CoconutsValue = Instance.new("IntValue")
	CoconutsValue.Name = "Coconuts"
	CoconutsValue.Archivable = true
	CoconutsValue.Parent = leaderstats
	local SeashellsValue = Instance.new("IntValue")
	SeashellsValue.Name = "Seashells"
	SeashellsValue.Archivable = true
	SeashellsValue.Parent = leaderstats
	local Data
	local PlayerID = "Player_"..plr.UserId
	Data = DataStore:GetAsync(PlayerID)
	if Data then
		CatfoodValue.Value = Data.CatFood
		XPValue.Value = Data.XP
		CoconutsValue.Value = Data.Coconuts
		SeashellsValue.Value = Data.Seashells
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Data = {
		CatFood = plr.leaderstats.CatFood.Value,
		XP = plr.leaderstats.XP.Value,
		Coconuts = plr.leaderstats.Coconuts.Value,
		Seashells = plr.leaderstats.Seashells.Value,
	}
	local PlayerID = "Player_"..plr.UserId
	DataStore:SetAsync(PlayerID,Data)
end)

It is because you are not getting a new data store for every player.

local DataStore = DataStoreService:GetDataStore("DataStore")
-- instead use
local DataStore = DataStoreService:GetDataStore("Player_"..player.UserId)
-- so every player will have their own data stores.

And you might want to check out my module.

Edit:

I saw this and this is not true. You should warp them in a pcall function just like so.

success, err = pcall(function()
     DataStore:SetAsync(PlayerID,Data)
end)
if success then
    -- our data saved successfully
else
    -- something wrong happened while saving data.
end

Also, SetAsync is not a good function to use, instead use UpdateAsync.
More info: GlobalDataStore | Roblox Creator Documentation

local DataStore = DataStoreService:GetDataStore("Player_"..player.UserId)

I don’t really know where to put that line since player is nil

If player is nil then where are you saving the data?

the local DataStore line is before the function(player) Unless I’m supposed to put the line inside of the playeradded function

You need to define it inside the function and use the player user id to get a unique key.

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
	local DataStore = DataStoreService:GetDataStore("Player_"..plr.UserId)
	local leaderstats = Instance.new("Folder",plr)
	leaderstats.Name = "leaderstats"
	leaderstats.Archivable = true
	leaderstats.Parent = plr
	local CatfoodValue = Instance.new("IntValue")
	CatfoodValue.Name = "CatFood"
	CatfoodValue.Archivable = true
	CatfoodValue.Parent = leaderstats
	local XPValue = Instance.new("IntValue")
	XPValue.Name = "XP"
	XPValue.Archivable = true
	XPValue.Parent = leaderstats
	local CoconutsValue = Instance.new("IntValue")
	CoconutsValue.Name = "Coconuts"
	CoconutsValue.Archivable = true
	CoconutsValue.Parent = leaderstats
	local SeashellsValue = Instance.new("IntValue")
	SeashellsValue.Name = "Seashells"
	SeashellsValue.Archivable = true
	SeashellsValue.Parent = leaderstats
	local Data
	local PlayerID = "Player_"..plr.UserId
	Data = DataStore:GetAsync(PlayerID,Data)
	if Data then
		CatfoodValue.Value = Data.CatFood
		XPValue.Value = Data.XP
		CoconutsValue.Value = Data.Coconuts
		SeashellsValue.Value = Data.Seashells
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local Data = {
		CatFood = plr.leaderstats.CatFood.Value,
		XP = plr.leaderstats.XP.Value,
		Coconuts = plr.leaderstats.Coconuts.Value,
		Seashells = plr.leaderstats.Seashells.Value,
	}
	local PlayerID = "Player_"..plr.UserId
	local success, err = pcall(function()
		DataStore:SetAsync(PlayerID,Data)
	end)
end)

The script looks like this now but at the line of DataStore:SetAsync(PlayerID,Data), DataStore is nil since it’s inside of the function.

No this is not how datastores work. You’re not suppose to create a datastore per player. Each player data is stored in 1 datastore.

2 Likes

Oops sorry, I meant keys. But he was still doing it wrong.

Let me fix this real quick,

local DataStore = DataStoreService:GetDataStore(key):GetAsync("Player_"..player.UserId)

Your script should be working , are API services on?

what does the key stand for? it’s just nil for me.

Yes, API services are on and always were.

It is where you save your data.

am I supposed to change the key to my datastore name?

local DataStore = DataStoreService:GetDataStore("TestKey"):GetAsync("Player_"..player.UserId)

Like this, if key changes, it will use the data store with the new key. If you change it back it will use that data.

1 Like

I got an error: ServerScriptService.DataStoreScript:27: attempt to call a nil value - Server - DataStoreScript:27

Here’s line 27:

	Data = DataStore:GetAsync(PlayerID,Data)
1 Like

Oh yes, remove data because there’s no data parameter in GetAsync.

1 Like

GetAsync returns data, you just need to the give PlayerID.