Hello , i’ve recently been trying to make a system that allows me to kick a player from my game if he isn’t in my server. I tried to find a thread about this but i couldn’t find anything like this. Any help would be very appreciated!
You’d want to use a web service, and poll it every X amount of seconds to get a map of newly banned players. Then check if any players in the server are banned, and if so, get rid of them.
Could you use DataStores for this as well?
In theory, yes. I was just about to edit my post about that. It’s a bad option and a hack, DataStore
s are only good at saving player data. It can certainly be done by polling a key (DataStore:OnUpdate()
is broken, go figure), but this is not a good solution and will prevent you from banning multiple players at once quickly in separate bans / kicks, or polling quickly.
Oops , i forgot about that…
What Avigant made sense, I think his method is a whole lot more reliable than using DataStores as of now.
It is definitely a better option, but not everyone has their own server.
It would work with datastores as well, the only problem with it would be OnUpdate as mentioned.
I think i just realised how to do it.
I’m preety sure this would work.
Server1: (ServerScriptService)
local kick = game:GetService("DataStoreService"):GetDataStore("KickData")
game.Players.PlayerAdded:Connect(function(plr)
while true do
wait(30)
if kick:GetAsync(plr.UserId) then
if kick:GetAsync(plr.UserId) == 1 then
plr:Kick("Oh no , you have been kicked.")
end
end
end)
Server2 (Admin) : (ServerScriptService)
local kick = game:GetService("DataStoreService"):GetDataStore("KickData")
game.ReplicatedStorage.Kick.OnServerEvent:Connect(function(player,plrkicked)
-- plrkicked: Text of a textbox which contains the userid of the player being kicked.
if player.Name == "TheFuzi" then
kick:SetAsync(plrkicked,1)
wait(30)
kick:SetAsync(plrkicked,0)
end
end)
Client (Admin):
script.Parent.MouseButton1Click:Connect(function()
game.ReplicatedStorage.Kick:FireServer(script.Parent.kicktext.Text)
end)
You should really be checking by account ID, not username.
Account names can be spoofed by exploits and if you change your username and someone changes their name to TheFuzi
, they can get in. Use the following code.
if player.UserId == 405445053 then
in server2.
Also, I highly recommend indenting your code, as this makes your code easier to read and debug. The Studio LUA editor does this by default, but there are websites such as Lua-beautify by blackmiaool or Lua beautifier that can indent code automatically.
While your point is valid that account IDs work better because they aren’t affected by name changes, it is not true that a player can change their player name from the server side of things. Changing their name on the client wouldn’t affect this (server-sided) method whatsoever.
Hi, it works but im not sure If it works in all servers
Yes. The script is using datastores, so the kick/ban will work on all servers.