Script not banning user

Issue?

Well I made an admin system and it works pretty well, although the banning situation… not so well- Whenever I press the ‘ban’ button after typing in the username and reason it doesnt ban, although the unban works.

Local script inside the ban button:

script.Parent.Parent.BanPresser.MouseButton1Click:Connect(function()
	local reason = script.Parent.Parent.ReasonBox.Text
	local plrtoban = script.Parent.Parent.UsernameBox.Text
	if reason.Text ~= "" and plrtoban ~= "" then
		game.ReplicatedStorage:WaitForChild("BAU"):FireServer(plrtoban, reason, "Ban")
	end
end)

Script inside serverscriptspace:

local admins = {"saouvs"}
local ds = game:GetService("DataStoreService"):GetDataStore("BAU")

game.Players.PlayerAdded:Connect(function(plr)
	
	local data = ds:GetAsync(plr.UserId)
	
	if data then
		local IsAdmin = false
		for i, v in pairs(admins) do
			if v == plr.UserId then
				IsAdmin = true
				game.StarterGui.samsadmin.BasicModeration:Clone().Parent = plr.PlayerGui
			end
		end
		if IsAdmin == false then
			if data[1] == true then
				plr:Kick("You have been banned from this game, reason:"..data[2])
			end
		end
	end
end)

game.ReplicatedStorage:WaitForChild("BAU").OnServerEvent:Connect(function(plr, plrtoban, reason, ban)
	if ban == "Ban" then
		for i, v in pairs(admins) do
			if plr.UserId == v then
				if game.Players:FindFirstChild(plrtoban) then
					if game.Players[plrtoban].UserId ~= plr.UserId then
						print(plrtoban.." has been banned!")
						game.Parent[plrtoban]:Kick("You have been banned from this game, reason: "..reason)
						ds:SetAsync(game.Players[plrtoban].UserId, {true, reason})
						else print("This player is an admin")
					end
				else
					local players = game:GetService("Players")
					local Id = players:GetNameFromUserIdAsync(plrtoban)
					if Id ~= plr.UserId then
						ds:SetAsync(Id, {true, reason})
						print(plrtoban.." has been banned!")
					end
				end
			end
		end
	elseif ban == "Unban" then
		local players = game:GetService("Players")
		local Id = players:GetUserIdFromNameAsync(plrtoban)
		ds:RemoveAsync(Id)
		print(plrtoban.." has been unbanned!")
	end
end)```

I'll try any and all solutions and suggestions, thank you.

In the admins table you put the names of the admins, but in the loop you set it to v == plr.UserId. Changing it to v == plr.Name should fix it.

1 Like

Are you sure, apparently it doesnt work, no errors are included either.

That’s weird I’ve also been having a lot of trouble with tables lately, don’t know if it’s Roblox related.

1 Like

Could be although depends as I made a table and it worked this morning, not sure but here’s what I put in just incase i got it wrong.


for i, v in pairs(admins) do
			if v == plr.Name then
				IsAdmin = true
				game.StarterGui.samsadmin.BasicModeration:Clone().Parent = plr.PlayerGui

I think it’s maybe because later on in the script you use GetNameFromUserId but input a name, you should use GetUserIdFromName instead.

1 Like

Testing, once again haha. Thanks again, lol-

Don’t forget the Async at the end.

1 Like

Unfortunately, again it’s decided not to work I don’t think I have another solution.

Here Parent has to replaced with Players.

Here you should change it again to v == plr.Name

If this doesn’t work, I don’t know what to do either.

2 Likes

Looks like it worked thank you so so so so much!

1 Like

Glad I helped, you’re welcome.

Alright, lol apologies for continuing this thread on if I were to make it group rank only where would I put if plr:GetRankInGroup(8176838) >= (210)?

In PlayerAdded

local IsAdmin = false
if plr:GetRankInGroup(8176838) >= 210 then
	IsAdmin = true
	game.StarterGui.samsadmin.BasicModeration:Clone().Parent = plr.PlayerGui
end
if IsAdmin == false then
	if data[1] == true then
		plr:Kick("You have been banned from this game, reason:"..data[2])
	end
end

and OnServerEvent

local players = game:GetService("Players")

game.ReplicatedStorage:WaitForChild("BAU").OnServerEvent:Connect(function(plr, plrtoban, reason, ban)
	if plr:GetRankInGroup(8176838) >= 210 then
		if ban == "Ban" then
			if game.Players:FindFirstChild(plrtoban) then
				if game.Players[plrtoban].UserId ~= plr.UserId then
					print(plrtoban.." has been banned!")
					game.Players[plrtoban]:Kick("You have been banned from this game, reason: "..reason)
					ds:SetAsync(game.Players[plrtoban].UserId, {true, reason})
				else
					print("This player is an admin")
				end
			else
				local Id = players:GetUserIdFromNameAsync(plrtoban)
				if Id ~= plr.UserId then
					ds:SetAsync(Id, {true, reason})
					print(plrtoban.." has been banned!")
				end
			end
		elseif ban == "Unban" then
			local Id = players:GetUserIdFromNameAsync(plrtoban)
			ds:RemoveAsync(Id)
			print(plrtoban.." has been unbanned!")
		end
	end
end)
1 Like