Multiple DataStore Script Not Working

im trying to save multiple currency
when i tested it, it didn’t save any currency
i tried to find the error but i couldn’t
please help

local datastoreservice = game:GetService(“DataStoreService”)
local playerdata = datastoreservice:GetOrderedDataStore(“playerdata”)

game.Players.PlayerAdded:Connect(function(player)

local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "cash"
cash.Parent = folder

local xp = Instance.new("IntValue")
xp.Name = "XP"
xp.Parent = folder

local playerId = 'player_'..player.UserId
local data = playerdata:GetAsync(playerId)
if data then
	cash.Value = data['cash']
	xp.Value = data['XP']
	print("data loaded")
else
	cash.Value = 0
	xp.Value = 0
	print("New player")
end

end)

local function createtable(player)
local playerstats = {}
for _, stat in pairs(player.leaderstats:GetChildren()) do
playerstats[stat.Name] = stat.Value
end
return playerstats
end

local function onplayerexit(player)
local playerstats = createtable(player)
local success, err pcall(function()
local playerId = ‘player_’…player.UserId
playerdata:SetAsync(playerId, playerstats)
end)
end

game.Players.PlayerRemoving:Connect(onplayerexit)

local success, err pcall(function()

This should be:
local success, err = call(function()

local playerId = ‘player_’…player.UserId

This would not combine strings properly. Replace it with only 1 period

Other than that code seems ok but I may have missed a few stuffs

Not one period. two period is correct ..

2 periods are used for string concatenation

My bad, use 2 periods. eeeeeeee

If it doesn’t work try to add
tostring()

local playerId = ‘player_’..tostring(player.UserId)

Thanks for the feedback but i found the problem its
local playerdata = datastoreservice:GetOrderedDataStore(“playerdata”)
it should be
local playerdata = datastoreservice:GetDataStore(“playerdata”)
but thanks tho :slight_smile: