Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a chat based ban script?

Hello.

I am working on an admin commands system for my game. I want to add a ban command, but I don't know how to get it to save. I tried using a remote function, datastorage, and a remote event. I'm not that familiar with saving things to the datastore that isn't stored inside of a player.

Here is part of my current script.

elseif msg:lower():sub(1,4) == ";ban" then
				local  obj = msg:sub(6)
			local found = game.Players:FindFirstChild(obj)	
			
			if found then
			game.Players[obj]:Kick("You have been banned!")
			else
			print(obj.." is not a player!"))
			end

This is kind of "requesty", but I'd appreciate an answer.

Thanks

1 answer

✓ Accepted answer

The code you have so far will successfully kick the player, but it will not ban them. To ban the player you need to save their UserId to a datastore. This can be done using the SetAsync() method of GlobalDatastore. Below is the code to save their UserId to the datastore.

local ds = game:GetService("DataStoreService"):GetDataStore("Bans") -- Gets the datastore itself so we can add to it.
ds:SetAsync(found.UserId, true) -- Adds the player to the datastore

This can be implemented into your current code like so:

elseif msg:lower():sub(1,4) == ";ban" then
	local  obj = msg:sub(6)
	local found = game.Players:FindFirstChild(obj)
	local ds = game:GetService("DataStoreService"):GetDataStore("Bans")  

	if found then
		ds:SetAsync(found.UserId, true)
		found:Kick("You have been banned!")
	else
		print(obj.." is not a player!"))
	end

But this is not enough to to totally ban the player. To ban the player we're going to need a script that checks if a player is on the Bans datastore everytime a player joins. I recommend putting this script into ServerScriptService to prevent any client access. This script is below.

game:GetService("Players").PlayerAdded:Connect(function(plr)
	local ds = game:GetService("DataStoreService"):GetDataStore("Bans") 
	
	if ds:GetAsync(plr.Userid) then
		plr:Kick()
	end
end)

Let me know if anything is wrong. If my answer helped you, make sure to mark it as correct!

4 comments

0

Thanks for replying. For some reason, when I try to test this on one of my alts, it says, "PLAYERNAME is not a player". Substitute PLAYNAME for my alts' name. Not sure why it is doing this.

0

I got rid of the FindFirstChild thing, and it works fine. Thanks for telling me what I should do.

0

No problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.