I am working on a ban script, however the issue is that when I set the ban to true, the player won’t be kicked unless they rejoin. To fix this, I was thinking of getting every player in every server, and if the UserId is the same as the ban UserId, they will be kicked.
Script so far (working):
script.Parent.Parent.Event.Event:Connect(function(text)
local moderator = script.Parent.Parent.Parent.Parent.Parent.Name
local userid = game:GetService("Players"):GetUserIdFromNameAsync(text)
local ds = game:GetService("DataStoreService")
local bans = ds:GetDataStore("bans")
local reason = script.Parent.Parent.reason.Text
local bankey = "ban"..userid
local banreason = "reason"..userid
local username = script.Parent.Parent.username.Text
bans:SetAsync("ban"..userid, true)
bans:SetAsync("reason"..userid, "You have been banned by "..moderator..": "..reason.." If you would like to appeal, or believe this was done by error, please DM @7z99_ on Twitter." )
end)
I suggest taking a look at this article. With such a system in place, it suffices to transmit the username across a dedicated bans topic, and letting every server run through their player list and kicking anyone who matches that ID.
Additionally, you could use TeleportService:GetPlayerInstanceAsync() to determine wether or not the player is actually in the game. You could then wait for ‘any’ server to message back confirming that the player was indeed kicked. It is possible for the service to mess up, this would help remediate that.
Why send anything other than a string when you can write everything as a string?
I was wrong! You can send anything over :PublishAsync()! Do check out the API reference. If something expects a Variant, that means you can send any arguments of any type. These arguments will all be provided to the callback.
-- For example the following
MessagingService:PublishAsync("Topic",userIdToBeBanned,reason,...)
-- will call the following callback:
function(userIdToBeBanned,reason,...)
-- (With ... being literally anything else you want to send along. You can add as many arguments as you want.)
Ran into an issue, I’m trying to ban myself, however I’m not being kicked.
Script:
m = game:GetService("MessagingService")
s = m:SubscribeAsync("Ban", function(id, reason)
for i,v in pairs(game.Players:GetChildren()) do
if v.UserId == id then
v:Kick(reason)
end
end
end)
What sends the ban:
local r = "You have been banned by "..moderator..": "..reason.." If you would like to appeal, or believe this was done by error, please DM @7z99_ on Twitter."
local m = game:GetService("MessagingService")
m:PublishAsync("Ban", userid, r)
Do any errors get returned? If not, try debugging the script. A print statement can point out what you are receiving if your SubscribeAsync callback gets called at all.
I myself see no reason for this not to work, though I cannot test. Try debugging the script, see what is happening. A reason may be that the server doesn’t get fired for messages it sends itself. I’ll have to check on this though.
I’ve bookmarked this post and will return to it tomorrow, in case you are still encountering issues.
The script might be receiving id as a string so the comparison v.UserId == id is matching different types. Cast id to a string using tostring to fix this issue.