Yet Another Issue | Ban System

Greetings, developers!

I created a ban system, however, when I chat the command, it doesn’t work. :thinking:
Everything seems correct, but it doesn’t work, with no errors. Heres the script:

local Players = game:GetService("Players")
local GroupId = 11941815

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("HGS_Bans902938948373")

game.Players.PlayerAdded:Connect(function(Player)
	local BanFolder = Instance.new("Folder", Player)
	BanFolder.Name = "BanValue"
	local BanValue = Instance.new("BoolValue", BanFolder)
	BanValue.Name = "Ban"
	
	Player.Chatted:Connect(function(Message)
		if Message[1] == "/ban" then			
			if Message[2] then
				local FoundPlayer
				
				for Index, Plr in ipairs(game.Players:GetPlayers()) do
					if (Plr.Name:sub(1,#Message[2]):lower() == Message[2]:lower()) then
						FoundPlayer = Plr
					end
				end
				
				FoundPlayer:WaitForChild("PlayerGui").BanScreen.Enabled = true
				BanValue.Value = true
				DataStore:SetAsync(Player.UserId, BanValue.Value)
			end
		end
	end)
end)

Any help is appreciated!

3 Likes

Is the variable Message a string? If so, use string.sub(Message, 1, 4) to get the first part of the command.

Or, you could do Message = string.split(Message, " ") and use Message[1] to get just the command.

3 Likes

Ah, still no errors, nor is it working…

are you sure the message is a table and not a string?

The message is supposed to be a string. (This is chat/string manipulation.)

this part makes me think there should be a table somewhere where the first input of that table is “/ban”
and the seccond is the player name

Yep, tables do use [1] and [2], etc… However! Chat manipulation also uses that to check parts of strings! Ex:

-- A player chats 2 parts. The parts are "Hello world!" How we get both the parts would be this:

Part[1] Part[2]

Now that’s just a simple way to put it, of course, you do have to change “part” to the message variable, and all that, but that will get both parts!


the chat system indeed split them. but chatted fires before the chat system does anything with it. even before the message is being filtered

Using this, to turn the string into a table use string.split(message, " ") to separate the words.

Using this table you can then use if message[1] == "/ban" then and find the player name with message[2]

1 Like

you have no split in the message. In short, it simply won’t work without string.split
string.split is spliting for text. Example:

local Message = "Hello world!"
local SplitedMessage = string.split(Message," ")

print("Splited message: ", SplitedMessage[1], SplitedMessage[2]) --will printed 'Hello' 'world!'
print("non-Splited message: ", Message[1], Message[2]) --will printed 'nil' 'nil'

Copy this and try this code in studio. I hope you have understood what the guys from above are saying about string.split

if you still don’t understand try this fixed code:

local Players = game:GetService("Players")
local GroupId = 11941815

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("HGS_Bans902938948373")

game.Players.PlayerAdded:Connect(function(Player)
	local BanFolder = Instance.new("Folder", Player)
	BanFolder.Name = "BanValue"
	local BanValue = Instance.new("BoolValue", BanFolder)
	BanValue.Name = "Ban"
	
	Player.Chatted:Connect(function(Message)
        local SplitedMessage = Message:split(" ")
		if SplitedMessage[1] == "/ban" then			
			if SplitedMessage[2] then
				local FoundPlayer
				
				for Index, Plr in ipairs(game.Players:GetPlayers()) do
					if (Plr.Name:sub(1,#SplitedMessage[2]):lower() == SplitedMessage[2]:lower()) then
						FoundPlayer = Plr
					end
				end
				
				FoundPlayer:WaitForChild("PlayerGui").BanScreen.Enabled = true
				BanValue.Value = true
				DataStore:SetAsync(Player.UserId, BanValue.Value)
			end
		end
	end)
end)
1 Like

Yeah, I found the issue a little bit ago. Thanks everyone!