Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to make a chat based ban script?

Asked by 5 years ago

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

Log in to vote
0
Answered by
oreoollie 649 Moderation Voter
5 years ago
Edited 5 years ago

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!

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. bluestreakejjp 41 — 5y
0
I got rid of the FindFirstChild thing, and it works fine. Thanks for telling me what I should do. bluestreakejjp 41 — 5y
0
No problem. oreoollie 649 — 5y
Ad

Answer this question