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

Ban somebody when they leave the game?

Asked by 3 years ago

i'm not sure if this is a thing or not is there a way to ban somebody when they leave?

1
Yes. Ziffixture 6913 — 3y
0
I just answered it! ViviTheLuaKing 103 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You can get their UserId and save it to a DataStore. By using this, when they rejoin, they will be kicked.

local datastore = game:GetService("DataStoreService"):GetDataStore("Ban")
game.Players.PlayersAdded:Connect(function(plr)
    local success,err = pcall(function()
        return datastore:GetAsync(plr.UserId)
    end)
    if success then
        plr:Kick("Banned.")
    else
        datastore:SetAsync(plr.UserId,"Banned")
    end
end)
0
Thanks! ViviTheLuaKing 103 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

If you want to ban someone when they leave the game, use PlayerRemoving. If you are confused, try this.

local Players = game:GetService("Players")
local dds = game:GetService("DataStoreService")
local banDataStore = dds:GetDataStore("BanData")

Players.PlayerRemoving:Connect(function(plr)
    local banned
    local success, err = pcall(function()
        banned = banDataStore:SetAsync(plr.UserId, true)
    end)
    if success then
        plr:Kick("You are banned from the game.")
    else
        print(err)
    end
end)

Players.PlayerAdded:Connect(function(plr)
    if banDataStore:GetAsync(plr.UserId) then
        plr:Kick("You are banned from the game.")
    end
end)

Answer this question