This gives me error 104: Cannot store dictionary in Datastore ... why?

I cannot really give backstory because I dont know the reasoning behind this?..

My code:

local dss = game:GetService("DataStoreService"):GetDataStore("tBanTestPrivate")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1):lower() == "&ban way 1m" then
			print('jh1')
			dss:SetAsync(plr.UserId, {BanTime = 60, currentServed = 0, timenow = os.time})
			print('jh2')
			plr:Kick()
			print('jh3')
		end
	end)
end)

jh1 prints, nothing else though.

1 Like

Try tostring(plr.UserId) instead of plr.UserId.

plr.UserId is the key, not apart of the dictionary, so I don’t believe it’s the problem.

But try it and tell me what happens.

{BanTime = 60, currentServed = 0, timenow = os.time}

I suggest you JSONEncode this a string before saving it. This is the issue.

I thought it was done automatically if I’m correct?..

I’m not sure if that’s entirely true. I would still try it and see if it works because I do something similar in a game of my own.

It rings me a bell that there was a problem when indexing datastore with integers that’s why I suggested my idea. Can you actually try it and tell me if it works?

Numeric keys are automatically converted into string. The error has nothing to do with the key and is a problem with what is being stored.

GlobalDataStore | Documentation - Roblox Creator Hub it says that the first parameter has to be a string.

Screenshot by Lightshot Its automatically converted

1 Like

It’s because you’re saving a function os.time instead of the value it returns. ‘os.time()’ should work.

3 Likes

I did not know that, thank you

1 Like

Replace os.time with os.time()

local dss = game:GetService("DataStoreService"):GetDataStore("tBanTestPrivate")

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		if msg:sub(1):lower() == "&ban way 1m" then
			print('jh1')
			dss:SetAsync(plr.UserId, {BanTime = 60, currentServed = 0, timenow = os.time()})
			print('jh2')
			plr:Kick()
			print('jh3')
		end
	end)
end)
3 Likes

Btw, I recommend wrapping the datastore function in a pcall.
Even though it succeeds most of the time, if it fails due time out or anything
it can cause your script to break.

DataStore information is coerced into a string format (potentially JSON) under the hood. JSON encoding data to a DataStore is redundant and unnecessary and it is certainly not required in order to save to DataStores.