Datastore `attempt to concatenate table with string`

I’m creating a ban function.

I have tried the perm ban, and it works flawlessly.
But the problem lies in the non perm ban.

I get the error required_asset_8288681721.MainModule:90: attempt to concatenate table with string.

My ban function looks like this:

function moonlightFramework.banPlayer(player, reason, datastoreName, perm, unixUnban)
	local DataStoreService = game:GetService("DataStoreService")

	local banDatastore = DataStoreService:GetDataStore(datastoreName)
	
	if perm == true then
		player:Kick("You have been banned forever for "..reason)
		print("Sucessfully banned "..player.Name.." forever for "..reason..".")
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." is banned?", true)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban reason?", reason)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban forever?", perm)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		print("Success on data save!")
	else
		player:Kick("You have been banned until "..os.date("*t", unixUnban).." for "..reason)
		print("Sucessfully banned "..player.Name.." until "..os.date("*t", unixUnban).." for "..reason..".")
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." is banned?", true)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban reason?", reason)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban forever?", perm)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." unban time?", unixUnban)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		print("Success on data save!")
	end
end
-- i dont mind if anyone takes this

and my server script looks like:

local moon = require(8288681721)

wait(3)
moon.banPlayer(game.Players:WaitForChild("callmehSpear"), "hacking", "banDatabse2", false, 1639999783)

As I said, when the perm parameter is set to true, it all works fine, but when it is false and I give it a unix timestamp, it doesn’t.

Cheers for any help?

1 Like

The error is what it is, you’re trying to put a table within a string. Convert the table into a single string then try to put it in.

I’m not as smart as I look, is itt he module’s script fault or the server script’s fault?

Yep it’s the module script’s fault.

Right, so reason is “hacking” so why and how is that a table…

Have you given all the modulescript code? The error is on line 90, but the code you’ve given only goes up to 52 lines.

No I haven’t. charcharcharhcahrhcahrhacr

There are ways to print a table like print("table: "..HttpService:JSONDecode(table)) and print("table", table)

Also I think the error occurs at the lines where os.date is used, because it does return a table and you’re using it directly.

Here it is on GitHub…

https://github.com/callmehSpear/Moonlight-Framework/blob/banBranch/MainModule.lua

local moonlightFramework = {}

-- General Utils

function moonlightFramework.getStatus()
	print("Fetching status...")
	local httpService = game:GetService("HttpService")
	local url = "https://api.github.com"
	local data = httpService:GetAsync(url .. "/repos/callmehSpear/Moonlight-Framework/releases")
	data = httpService:JSONDecode(data)
	warn("Moonlight Framework v"..data[1].tag_name.." is running.")
	local ver = data[1].tag_name
	return ver
end

-- Point System, for airlines, cafes, hotels etc

function moonlightFramework.postPoints(player, amountOfPoints, datastoreName)
	local DataStoreService = game:GetService("DataStoreService")

	local pointDatastore = DataStoreService:GetDataStore(datastoreName)

	local success, errorMessage = pcall(function()
		pointDatastore:IncrementAsync(player.UserId, amountOfPoints)
	end)
	if not success then
		warn("WARNING!!! Data did not save! "..errorMessage)
	end
	print("postPoints Success! Player ID: "..player.UserId.." Amount of Points added: "..amountOfPoints.." Datastore Name: "..datastoreName)
end
function moonlightFramework.getPoints(player, datastoreName)
	local DataStoreService = game:GetService("DataStoreService")

	local pointDatastore = DataStoreService:GetDataStore(datastoreName)

	local success, currentPoints = pcall(function()
		return pointDatastore:GetAsync(player.UserId)
	end)
	if success then
		return currentPoints
	end
end
function moonlightFramework.resetPoints(player, datastoreName)
	local DataStoreService = game:GetService("DataStoreService")

	local pointDatastore = DataStoreService:GetDataStore(datastoreName)

	local success, errorMessage = pcall(function()
		pointDatastore:SetAsync(player.UserId, 0)
	end)
	if not success then
		warn("WARNING!!! Data did not reset! "..errorMessage)
	end
	print("resetPoints Success! Player ID: "..player.UserId.." Datastore Name: "..datastoreName)
end

function moonlightFramework.kickPlayer(player, reason)
	player:Kick("You have been kicked for "..reason)
	print("Sucessfully kicked "..player.Name.." for "..reason..".")
end

function moonlightFramework.banPlayer(player, reason, datastoreName, perm, unixUnban)
	local DataStoreService = game:GetService("DataStoreService")

	local banDatastore = DataStoreService:GetDataStore(datastoreName)
	
	if perm == true then
		player:Kick("You have been banned forever for "..reason)
		print("Sucessfully banned "..player.Name.." forever for "..reason..".")
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." is banned?", true)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban reason?", reason)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban forever?", perm)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		print("Success on data save!")
	else
		player:Kick("You have been banned until "..os.date("*t", unixUnban).." for "..reason)
		print("Sucessfully banned "..player.Name.." until "..os.date("*t", unixUnban).." for "..reason..".")
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." is banned?", true)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban reason?", reason)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." ban forever?", perm)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		local success, errorMessage = pcall(function()
			banDatastore:SetAsync(player.UserId.." unban time?", unixUnban)
		end)
		if not success then
			warn("WARNING!!! Ban did not save! "..errorMessage)
		end
		print("Success on data save!")
	end
end

return moonlightFramework

Try changing line 90

player:Kick("You have been banned until "..os.date("*t", unixUnban).." for "..reason)

To

player:Kick("You have been banned until "..os.date("%c", unixUnban).." for "..reason)

That bans me, atleast, but I still get the error:

required_asset_8288681721.MainModule:91: attempt to concatenate table with string

and doesn’t run

print("Success on data save!")

— could it becausse they ahve left the game and datacant save anymore as server shut down?

Change the *t on line 91 as well

print("Sucessfully banned "..player.Name.." until "..os.date("%c", unixUnban).." for "..reason..".")
1 Like

os.date returns a table and you can’t combine it with a string. You need to do something like this
" until “…os.date(”*t", unixUnban).month…" for "

1 Like

Using the %c formatstring, it actual does return a string
os date c

3 Likes

Well I tried combining os.date() with strings and I got the same error. Perhaps you could try using a custom function to convert date to string depending on how you want it to visualize it:

function toString(date)
	local priority = {"year", "month", "day", "hour", "min"}
	local result = "" 
	for i, v in pairs(priority) do 
		result ..= " "..v..":"..date[v]
	end
	return result 
end

local current = os.date("*t", os.time())
print(toString(current))
1 Like

Thank you, it all works now, cheers guys.

2 Likes

Wow that’s dumb. I mean it’s nice but a total pain to document correctly. The wiki just says it returns a table, but if that’s the case it should say it returns a variant. Do you think that the documentation needs to be fixed there?

1 Like

Yeah, but IMO Variant should just be replaced with what something could return. The documentation does say that inputting *t or !*t will make os.date return a dictionary, but it should still be more apparent that os.date can return both string and dictionary.

1 Like