Warning system Sensor does it for everything, even if it isn't inappropriate

This script isn’t working proper. It is supposed to sensor anything bad but this happens instead…

Script:

local GroupId = 6741421
local MinimumRankToUseCommand = 9

function f(message)
	local TextService = game:GetService("TextService")
	local RunService = game:GetService("RunService")
	if RunService:IsStudio() then return message end
	local Success, Error = pcall(function()
		message = TextService:FilterStringAsync(message, game.CreatorId)
	end)
	if Success then
		local FilterSuccess, FilterError = pcall(function()
			message = message:GetNonChatStringForBroadcastAsync()
		end)
		if FilterSuccess then
			return message
		end
	end
	local result = ""
	for i = 1, string.len(message) do
		result = result .. "#"
	end
	return result
end

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local WarningGUI = script.WarningGUI:Clone()
		WarningGUI.Parent = Character.Head
	end)
	
	Player.Chatted:Connect(function(Message)
		local SplitMessage = Message:split(" ")
		if SplitMessage[1] == "!warn" and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
			local NameOfPlayerToWarn = SplitMessage[2]
			local PlayerToWarn = game.Players:FindFirstChild(NameOfPlayerToWarn)
			local Reason = f(Message:split(NameOfPlayerToWarn)[2])
			
			local WarningGUI = PlayerToWarn.Character.Head.WarningGUI
			local CurrentWarnings = WarningGUI.Warnings
			
			CurrentWarnings.Value = CurrentWarnings.Value + 1
			WarningGUI.WarningLabel.Text = "W" .. CurrentWarnings.Value .. " - " .. Reason
			
			if CurrentWarnings.Value >= 3 then
				PlayerToWarn:Kick("You've reached the maximum number of warnings and have been kicked from the server.")
			end
		end
	end)
end)

How do I fix this?

1 Like

Have you tried putting prints of the values of variables in to confirm the values are as expected in each section of the code?

1 Like

No I have not tried that yet. How might I do it?

after a process which is followed by an if put a print(“variableName=”,variable) between them.
So when the pcall for FilterStringAsync has completed a print of the Success variable after that will confirm the if will be executed.
Same goes for the pcall for FilterSuccess after getNonChatStringForBroadcaseAsync and the value contained in SplitMessage[1] in the f function.