#Created a simple permanant ban command. Help me optimize my code

1 messages · Page 1 of 1 (latest)

compact lichen
#

I made a script to ban/unban people. I want to know how to optimize it so next time, i will know how to write clean code.

#

-Local Script

local Txt = game:GetService("TextChatService")

local Commands = {
    ["ban"] = function(msg)
        game.ReplicatedStorage.Ban:FireServer(msg, true)
        print("Sended")
    end, 
    ["unban"] = function(msg)
        game.ReplicatedStorage.Ban:FireServer(msg, false)
        print("Sended")
    end,
}

Txt.OnIncomingMessage = function(msg)
    local SplitedMsg = string.split(msg.Text, " ")
    if SplitedMsg[1] ~= "/f" then return end
    Commands[SplitedMsg[2]](SplitedMsg)
end```
#

-Script


local template = {
    Banned = false,
    Reason = "None"
}

local function ChangeState(State, DataStore)
    while DataStore.State == false do
        if DataStore:Open(template) ~= "Success" then print("Failed to open. Trying again in 5s ...") task.wait(5) continue end
    end
end

game.Players.PlayerAdded:Connect(function(plr)
    
    local DataStore = DataStoreModule.new("Banned", plr.UserId)
    DataStore.StateChanged:Connect(ChangeState)
    ChangeState(DataStore.State, DataStore)
    
    repeat wait() until DataStore.State == true
    
    if DataStore.Value.Banned == true then
        plr:Kick("You are banned for the following reason:\n".. DataStore.Value.Reason)
    end
    
end)

game.ReplicatedStorage.Ban.OnServerEvent:Connect(function(mod, msg, ban)
    local PlayerName = msg[3]
    local UserId = game.Players:GetUserIdFromNameAsync(PlayerName)
    local DataStore = DataStoreModule.new("Banned", UserId)
    local reason = msg[4] or "None"
    DataStore.StateChanged:Connect(ChangeState)
    ChangeState(DataStore.State, DataStore)
    repeat wait() until DataStore.State == true
    DataStore.Value.Banned = ban
    DataStore.Value.Reason = reason
    if ban == true then
        game.Players:GetPlayerByUserId(UserId):Kick("An error occured. Please rejoin the game")
    end
end)```
#

-Files

#

-Structure of a command: /f ban {username} {reason}

tacit moon
#

Any client can fire a remote event so if you don’t have any validation checks to make sure it’s valid then any random exploiter could ban anyone. Also you do the chat detection on the server with player.Chatted:Connect.

compact lichen