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
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!