No errors but my script won't work

Hello, I was making a admin for my game, and it’s don’t work.

local DDS = game:GetService("DataStoreService"):GetDataStore("Simplified_01")

game.Players.PlayerAdded:Connect(function(plr)
	if DDS:GetAsync(plr.UserId.."-BAN",true) then
		plr:Kick("\n[Banned]\nYou have a active ban recorded!")
	else
		
	if plr:GetRankInGroup(12612601) >= 8 then
		plr.Chatted:Connect(function(msg)
			local split = msg:lower():split(" ")
			local lower = msg:lower()
			if split[1] == "kick" then
				local target = game:GetService("Players"):FindFirstChild(split[2])
				local reason = msg:split(split[2].." ")[2]
				
				if reason ~= nil then
					target:Kick("\n[Kicked]\n[Moderated by]: "..plr.Name.."\n[Reason]: "..reason)
				else
					target:Kick("\n[Kicked]\n[Moderated by]: "..plr.Name.."\n[Reason]: No reasons have been specified by "..plr.Name..".")
			if split[1] == "ban" then
						local target = game:GetService("Players"):FindFirstChild(split[2])
						local BanReason = msg:split(split[2].." ")[2]
						
						DDS:SetAsync(target.UserId.."-BAN",true)
							target:Kick("\n[Banned]\nA admin has decided to ban you.")
					end
				end
			end
		end)
	end
end

If there’s no error, it’s likely that one of your if statements is failing. Put prints after every single if function so you can see which one doesn’t print. That’s the if statement you need to focus on. Just do some debugging first.

By debugging, I found a quite interesting error.


ServerScriptService.SimplifiedAdmin:31: Expected 'end' (to close 'function' at line 3), got <eof>; did you forget to close 'then' at line 20?

Someone help… Like, please, I really want to do this script…

There are a few issues in the code that are causing problems:

First: because the chatted message is converted into lowercase, any player with capital letters in their name would be unable to be found. A more robust handler needs to be written.

Second: players can never be banned by this script because the logic to perform a ban is checked for and executed in a block that only runs if the command is to kick. These two code blocks need to be separated.

Here is something that should work:

local DDS = game:GetService("DataStoreService"):GetDataStore("Simplified_01")
local players = game:GetService("Players")

local CHECK_DISPLAY_NAMES = false

local function find_player(name)
	local player_named
	
	local player_list = players:GetPlayers()
	for _,player in ipairs(player_list) do
		if string.lower(player.Name) == name or (CHECK_DISPLAY_NAMES and string.lower(player.DisplayName) == name) then
			player_named = player
			break
		end
	end
	return player_named
end

players.PlayerAdded:Connect(function(plr)
	local success, result = pcall(DDS.GetAsync,DDS,plr.UserId.."-BAN")
	if success and result then
		plr:Kick("\n[Banned]\nYou have a active ban recorded!")
	else

		if plr:GetRankInGroup(12612601) >= 8 then
			plr.Chatted:Connect(function(msg)
				local split = string.split(msg:lower()," ")
				
				local ban = split[1] == "ban"
				local kick = ban or split[1] == "kick"
				
				if kick then
					local target = find_player(split[2])
					if not target then return end	--Exit the function if the player cannot be found.
					
					--Rather than run a new split, you can stitch the original back together.
					--Unlike the original implementation, this will return an empty string rather than nil if no reason is provided.
					local reason = table.concat(split," ",3)
					
					if ban then
						--Banning in this block.
						DDS:SetAsync(target.UserId.."-BAN",true)
						target:Kick("\n[Banned]\nA admin has decided to ban you.")
					else
						--Kicking only in this block.
						if reason ~= "" then
							target:Kick("\n[Kicked]\n[Moderated by]: "..plr.Name.."\n[Reason]: "..reason)
						else
							target:Kick("\n[Kicked]\n[Moderated by]: "..plr.Name.."\n[Reason]: No reasons have been specified by "..plr.Name..".")
						end
					end
				end
			end)
		end
	end
end)

This code finds players by searching through all players and converting their names to their lowercase counterparts before making a comparison. It also has and optional flag to check DisplayNames that is currently turned off. Banning has also been pulled out of the kicking logic, so it should run.

2 Likes