Unable to cast array (Datastore)?

Hello Developers,
So i was making this datastore but when the user left, it will save the datastore. But my problem is in this line, return AccountService:SetAsync(player.UserId..'-id:'..'-[-]',AccountService:GetAsync(player.UserId..'-id:'..'-[-]')). it says Unable to cast array. I’ve tried searching on the web [Unable to cast to array] but doesn’t match with my problem.

local function player_LEFT(player)
	--[[ Output: table ]]print(typeof(AccountService:GetAsync(player.UserId..'-id:'..'-[-]')))
	local success, err = pcall(function()
		return AccountService:SetAsync(player.UserId..'-id:'..'-[-]',AccountService:GetAsync(player.UserId..'-id:'..'-[-]'))
	end)
	--[[ Output: Unable to cast array ]]print(err)
	if success then
		warn('Data saved successfully for '..player.Name)
	else
		warn('Data saved unsuccessfully for '..player.Name.." we're really sorry :(")
	end
end

Thank you so much for helping me, i really appreciate it!

1 Like

You’re calling DataStore methods way too often. Do you not have a session cache? You should only ever be using GetAsync when you need to load player data or read an offline player’s data and UpdateAsync when you need to save a player’s data during autosaving, critical save points (e.g. product purchase) or upon leaving the experience.

I don’t know the source of your error but I’d honestly look at the root rather than try guiding you towards a bandaid solution blindly. You need to change the way you handle player data; you should already have a table that you can just pass to SetAsync here. And on that note that begs the question of why you’re just saving back the same data they already have stored? That doesn’t make sense.

3 Likes