Ban script help?

The ban script doesn’t appear to be kicking the player and the ban handler doesn’t work?

ban script:

ban.OnServerEvent:Connect(function(player, playerToBan, reason)
	if not table.find(ADMIN, player.UserId) then return end
	playeruserID = getUserIdFromUsername(playerToBan)
	if playeruserID == 1 then
		print("Cannot Ban This User!")
		return
		else
		local success, errormessage = pcall(function()
			BanData:SetAsync(playeruserID, true, reason)
		end)
		if success then
			print("yes")
			if game.Players:FindFirstChild(player) then
				player:Kick(reason)
			else
				return
			end
		end
	end
end)

and the ban handler:

local manualbans = {}

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

game.Players.PlayerAdded:Connect(function(player)

	local playerID = player.UserId

	if manualbans[playerID] then
		player:Kick("You have been banned from the game! Appeal in our disc server.")
	end

	local banned
	local success, errormessage = pcall(function()
		banned = banData:GetAsync(playerID)
	end)

	if banned == true then
		player:Kick("You have been banned from the game! Appeal in our disc server.")
	end
end)

I saw this post : Need help with ban script but couldn’t get anything out of it. I appreciate any help!

edit : accidentally put link in lua

2 Likes

You should be checking if it successfully got the data. You should also be checking if there is any actual data and you should set default data if there isn’t

1 Like

No… It doesn’t appear to be being added and the player isn’t getting kicked.

edit : I was wrong.

1 Like

The data store are exactly the same in the name of it, correct?

This block might be problematic, since the player was already listed: playerToBan and that player is a player object referencing to the requester.

1 Like

Yes, the datastore names are the exact same.

The obvious outlier is this: game.Players:FindFirstChild(player)

Just change the block of:

if game.Players:FindFirstChild(player) then
	player:Kick(reason)
else
	return
end

…to this…

if playerToBan.Parent then
	playerToBan:Kick(reason)
else
	return
end
2 Likes

I changed it to this :

if success then
			print("yes")
			if not game.Players:FindFirstChild(playerToBan) then return end
			else
				playerToBan:Kick(reason)
			end
		end
end)

And it said that i tried to call a nil value at playerToBan:Kick
It did not print anything.

1 Like

I responded too late, the message above was posted at the same time as yours

1 Like

It did not appear to work, the button is not doing anything.

1 Like

You can try and add the banned player’s ID into a table and then everytime he joins, The game tries to see if the player’s ID matches with the banned ID to actually kick him again.

1 Like

Oh, apparently there’s another issue. You cannot send instances over remotes and you’ll have to use a string reference of the user’s username(not always consistent though). You can fire their UserId instead and use this API to get the player(to ban):

local playerToBan = Players:GetPlayerFromUserId(targetPlayerUserId)
if playerToBan then
	playerToBan:Kick()
end
return
1 Like

Huh, I did not know of that! Though even after applying that… It still doesn’t do anything?

ban.OnServerEvent:Connect(function(player, playerToBan, reason)
	if not table.find(ADMIN, player.UserId) then return end
	playeruserID = getUserIdFromUsername(playerToBan)
	if playeruserID == 1 then
		print("Cannot Ban This User!")
		return
		else
		local success, errormessage = pcall(function()
			BanData:SetAsync(playeruserID, true, reason)
		end)
		if success then
			local playerToBan = Players:GetPlayerFromUserId(playeruserID)
			if playerToBan.Parent then
				playerToBan:Kick(reason)
			else
				return
			end
		end
	end
end)

I think this is nil, check the argument if it isn’t invalidated by the remote, haha.

I’m not sure, the localscript is fine?

maincmd.Ban.MouseButton1Click:Connect(function()
	rp.Ban:FireServer(Target.Text, Reason.Text)
end)

and the other remotes that i have these types of arguments in worked?

What’s the intended input here?

Oh, okay. I tried printing playeruserid but when I pressed the button it didn’t print anything…

It’s supposed to input username, then it gets transformed into userid through useridfromnameasync and then sets the id to true and the player is kicked, ban handler kicks them again when they rejoin.

May I see this function though?

1 Like
local cache = {}
function getUserIdFromUsername(name)
	if cache[name] then return cache[name] end
	local player = Players:FindFirstChild(name)
	if player then
		cache[name] = player.UserId
		return player.UserId
	end 
	local id
	pcall(function ()
		id = Players:GetUserIdFromNameAsync(name)
	end)
	cache[name] = id
	return id
end

I got it from here. Players | Roblox Creator Documentation
From the roblox’s example which shows us how to use it.

I still have no clue whatsoever that could be the problem. Maybe the client script isn’t working as intended? Try debugging the issue there by printing Target.Text.