Why is table not printing?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to insert UserId into table in every server and be able to save the table

  2. What is the issue? Include screenshots / videos if possible!
    The issue is it warns Could not save data!. That means that it didn’t save.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried printing the table and seeing if it insert when leaving. That worked. And I tried printing DataStore:GetAsync() but it returns nil.

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

local function getHighestNumber(numbersTable)
	local highestNumber = nil
	if numbersTable[1] == nil then
		highestNumber = 1
	elseif numbersTable[1] ~= nil then
		for i,v in pairs(numbersTable) do
			if	not highestNumber or i > highestNumber then
				highestNumber = i
			end
		end
	end
	
	return highestNumber
end

MessagingService = game:GetService("MessagingService")
t = {}
game.Players.PlayerAdded:Connect(function(player)
	local data = DataStore:GetAsync("Datas")
	print(data)
	if data ~= nil then
		print(data[576101447])
	end
	
	if table.find(t, player.UserId) then
	else
		
		if t[1] == nil then
			
			MessagingService:PublishAsync("AddedPlayer", player.UserId)
		elseif t[2] ~= nil then
			
			
			MessagingService:PublishAsync("AddedPlayer", player.UserId)
		end

	end
	

end)


MessagingService:SubscribeAsync("AddedPlayer", function(data)
	local playerUserId = data.Data
	local MaxIndex = getHighestNumber(t)
	local PlayerName = game.Players:GetPlayerByUserId(playerUserId)
	t[playerUserId] = PlayerName
	
	local success, err = pcall(function()
		DataStore:SetAsync("Datas", t) 

	end)

	if not success then

		warn('Could not save data!')

	end
	print(t)

end)



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

	print(DataStore:GetAsync("Datas"))	
	local success, err = pcall(function()
		DataStore:SetAsync("Datas", t) 

	end)
	if not success then
		warn('Could not save data!')
	end
	print(t)
end)


This the script

Use print(err) to see why it doesn’t work.
My guess would be you haven’t enabled studio access to API services.

it says 104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8
I enabled API services when I first started the game

I will use ProfileService instead, I think it will be easier

This means what you’re trying to save has a special character that isn’t accepted.

I will save a numbervalue that has the UserId

Are you saving the value or the instance?

Why are you using messaging service for this?

So I can replicate the table to other servers

why are you setting the datastore again after…

So it wil update the Datastore, I will try UpdateAsync

1 Like

It says Unable to cast value to function

1 Like

What are you trying to use that for? Datastores automatically update on all servers.

1 Like

All you have to do is use HttpService:JSONEncode() on the value you are saving and then when you read the datastore run HttpService:JSONDecode() to turn in into table when reading for example

-- Setting the ds

local tablee = {}
local HttpService = game:GetService("HttpService")
local ds = game:GetService("DataStoreService"):GetDatastore("DSName")

ds:SetAsync(Plr.UserId,HttpService:JSONEncode(tablee))

-- Reading the ds

local Data = HttpService:JSONDecode(ds:GetAsync(Plr.UserId))
print(Data)
1 Like

I barely use HttpService so ima start using it more. Thank you

1 Like