[Solved] Function fails to return a result, or a result is unable to be gathered

I have a function that is meant to temporarily ban players with given parameters. I have tried solutions from around devforum but to no avail. Here is the function code and the code calling the function.
Other functions work just fine…

-- CALLED FUNCTION --
function addBanName(plr, plrtoBan, reason, banlength)
	local returning
	local id = getId(plrtoBan)
	if id then
		if id ~= "error" then
			if math.floor(banlength) < 3 then
				local info
				local success, err = pcall(function()
					bans:SetAsync(id .. "_Banned", {
						["Banner"] = plr,
						["Date"] = os.date("%d") .. "/" .. os.date("%m") .. "/" .. os.date("%y") .. " at " .. os.date("%H") .. ":" .. os.date("%M") .. " UTC",
						["Reason"] = game:GetService("Chat"):FilterStringForBroadcast(reason, plrService:FindFirstChild(plr)),
						["Victim"] = plrtoBan,
						["Appealable"] = "no",
						["TempBan"] = math.floor(banlength) .. " day(s)",
						["TempBanNumber"] = os.time() + math.floor(day * math.floor(banlength))
					})
				end)
				
				if success then
					if plrService:FindFirstChild(plrtoBan) then
						game:GetService("TeleportService"):Teleport(7874256009, plrService:FindFirstChild(plrtoBan))
						wait()
						plrService:FindFirstChild(plrtoBan):Kick("You have been temporarily banned. Please wait to be teleported for more information.")
						returning = true
					end
				else
					returning = false
				end
			else
				local info
				local success, err = pcall(function()
					bans:SetAsync(id .. "_Banned", {
						["Banner"] = plr,
						["Date"] = os.date("%d") .. "/" .. os.date("%m") .. "/" .. os.date("%y") .. " at " .. os.date("%H") .. ":" .. os.date("%M") .. " UTC",
						["Reason"] = game:GetService("Chat"):FilterStringForBroadcast(reason, plrService:FindFirstChild(plr)),
						["Victim"] = plrtoBan,
						["Appealable"] = "yes",
						["TempBan"] = math.floor(banlength) .. " day(s)",
						["TempBanNumber"] = os.time() + math.floor(day * math.floor(banlength))
					})
				end)

				if success then
					if plrService:FindFirstChild(plrtoBan) then
						game:GetService("TeleportService"):Teleport(7874256009, plrService:FindFirstChild(plrtoBan))
						wait()
						plrService:FindFirstChild(plrtoBan):Kick("You have been temporarily banned. Please wait to be teleported for more information.")
						returning = true
					end
				else
					returning = false
				end
			end
		else
			returning = false
		end
	else
		returning = false
	end
	
	return returning
end
-- CALLED FUNCTION --

-- CALLING FUNCTION --
local addbannow = addBanName(plr.Name, name, reasonstring, banlength)
local requests = 0

repeat wait()
	requests += 1
until addbannow or requests == 200

if addbannow == true then
 game:GetService("ReplicatedStorage").SendChatMessage:FireClient(plr, "Successfully banned user " 
 .. name .. " with ID " .. getId(name) .. "\nLength: " .. banlength .. " days\nReason: " .. reasonstring)
 debounceC[plr.Name]:Destroy()
else
	game:GetService("ReplicatedStorage").SendChatMessage:FireClient(plr, "There was an error banning this player.\nPlease try again.")
debounceC[plr.Name]:Destroy()
end
-- CALLING FUNCTION --
1 Like

What does the function currently return?

This might be your issue as well. I prefer to have a boolean instead of an actual instance, so instead of this, do debounceC[plr.Name] = nil or whatever value.

It returns true if everything was successful with adding the ban, and if any errors or abnormalities show (if statement) it returns false. Then the calling function block sorts out the “successful” message to the person banning the player.

The debounce works perfectly fine and is my way of percieving things. The debounce has nothing to do with this function error.

Okay, so what is the function currently returning?

Would you mind showing the GetId() function and / or the full code, that might help

^ The full code is over 380 lines.

I am running this function for a player who is not currently in-game. And with this information, I have now realised my mistake. If the player is not in the game but it was a success, I have not seen it and forgot to add returning = true to those areas. Thus not responding with anything at all. I will try this when I am next available and will mark a solution once tested.

Therefore I can mark this thread as solved.

1 Like