Logging kicks inside a datastore

I know that there are several other posts about this but i couldn’t read any help out of these, so i am trying to achieve something that logs a kick into a datastore, which means i’d have to insert a table into a datastore table, the problem is that i don’t know how to do this.

    --Script above is not important.
	else
		local LogsTable
		local LogsTemplate = {[os.date('%c',os.time())] = "Invoked AdminFunction without the matching permission."}
		local Success,ErrorMessage = pcall(function()
			LogsTable = KicklogDatastore:GetAsync(KicklogKey.. Player.UserId)
		end)
		if Success then
			if LogsTable then
				table.insert(LogsTable,#LogsTable+1,LogsTemplate)
				local Success,ErrorMessage = pcall(function()
					KicklogDatastore:SetAsync(KicklogKey..Player.UserId,LogsTable)
				end)
				if not Success then
					warn("Error: "..ErrorMessage)
				end
			elseif not LogsTable then
				local Success,ErrorMessage = pcall(function()
					KicklogDatastore:SetAsync(KicklogKey..Player.UserId,LogsTemplate)
				end)
				if not Success then
					warn("Error: "..ErrorMessage)
				end
			end
		elseif not Success then
			warn("Error: "..ErrorMessage)
		end
		Player:Kick("\nClient tried to invoke a RemoteFunction which the client has no access to.\n\n[ These actions have been logged ]")
	end
end

I was thinking of fetching the datastore and if the datastore has data that it will insert with table.insert() LogsTemplate into the datastore, but i have read alot that it would cause datastore errors using table.insert, i don’t know any other way of doing this.

Use getAsync to retrieve the table, add the value to the local table, update the datastore.

Do that, also, you forgot to make sure if the previous datastore IS a table.

Edited: I posted too soon.

1 Like

There is a special function for tables when it comes to adding and removing. I do however suggest that you use metamethods and metables instead as they have more control.

Setting Data:

local testTable = {} -- empty table

table.insert(testTable, #testTable + 1, {PlayerName = Player.Name, banTimeInSeconds = 50})

KicklogDataStore:SetAsync(plr.UserId, testTable)

Retrieving Data:

local data = KicklogDataStore:GetAsync(Player.UserId)

if data  and  typeof(data) == 'table' then
if data[Player.Name] then
Player:Kick("You are banned.")
end

I do suggest using UpdateAsync instead but for this post I don’t want to go to in depth.

2 Likes

Works perfectly, thanks.

1 Like