How would I go about kicking someone from a different server?

I am looking for a way to kick someone with a ban gui after the ban button has been clicked by an admin.

I am using the messaging service atm, but I am having issues getting it to kick people after the PublishAsync() is sent.

I have some code samples here.

Firstly, the publishing side;

local publishSuccess, publishResult = pcall(function()
						 
        MessagingService:PublishAsync(MESSAGING_TOPIC, tostring(plr2))
end)
	if not publishSuccess then
		print(publishResult)
	end

Secondly, the receiving side;

local subSuccess,subConnection = pcall(function()
     return MessagingService:SubscribeAsync(MESSAGING_TOPIC,function(message)
local p = game.Players:GetUserIdFromNameAsync(message.Data)
		local plr = game.Players:GetPlayerByUserId(p)
				
				if plr then
					plr:Kick()
				end
			end)
		end)

		if subSuccess then
			print("Success.")
		end

If anyone can help me here, that would be great!

2 Likes

What does plr2 equal? It obviously can’t equal a Player object since the Player would be in a different Server at time of calling, so what is it exactly, and why tostring()?

Can you also confirm there are no errors in either Server’s output?

plr2 = game.Players:GetPlayerFromUserId(message.Data)

and yes, there are no output errors

This is where the publish originates

remotes.Ban.OnServerEvent:Connect(function(sender, banned, reason)
	local plr = game.Players:GetPlayerByUserId(banned)

	local plr2 = game.Players:GetNameFromUserIdAsync(banned)

	for i, v in pairs(admins) do
		if sender.Name == v then
			if plr2 == v then
				warn("Player is admin, cannot ban")
			else
				local user = Instance.new("StringValue")
				user.Name = "user"
				user.Parent = game.ServerStorage:WaitForChild("Bans")
				user.Value = tostring(banned)

				DS1:SetAsync(banned,user.Value)

				if game.Players:FindFirstChild(plr2) then
					plr:Kick(reason)
					game.ReplicatedStorage.OtherRemotes.Banned:FireAllClients(plr2)
					
					local publishSuccess, publishResult = pcall(function()
						MessagingService:PublishAsync(MESSAGING_TOPIC, tostring(plr2))
					end)
					if not publishSuccess then
						print(publishResult)
					end
				else
					game.ReplicatedStorage.OtherRemotes.Banned:FireAllClients(plr2)
					
					local publishSuccess, publishResult = pcall(function()
						MessagingService:PublishAsync(MESSAGING_TOPIC, tostring(plr2))
					end)
					if not publishSuccess then
						print(publishResult)
					end
				end
			end
		else
			warn("Player requesting user ban is not admin")
		end
	end
end)

If you have checked for errors on both ends, and see nothing, then I’m not sure what the issue is. I might be missing something, but I don’t see anything wrong syntax wise.

The next thing I would try is placing prints everywhere around your code to see what runs, and to confirm variables are what you think they are. A good first step would be to check if the message goes through by printing something right inside of the SubscribeAsync function.

I’ll see if that works, and get back to you about it

Okay, so I removed that while loop, and I added a subConnection:Disconnect() below the print(“Success.”)

It never printed success, that is where the issue is

Try removing all pcalls so that you can view the errors, or print the error returned by the pcall.

I found where it is printing Success, but it is doing it too early. How would I make it do the pcall when it finds the player?

1 Like

I’m not sure I understand. Can you explain your question again?

Im lowkey confused on your code but try this

local function kickPlayer(name)
   --do your kick player code here
end
MessagingService:SubscribeAsync(MESSAGING_TOPIC,kickPlayer(message)

havent done messaging service in a while so there might be errors

1 Like

The printing of success and the disconnect are happening too early. I need it to kick the player THEN disconnect

This may work, I’ll try it when I wake up

It didn’t work. kickPlayer(message) is saying that “message is an unknown global”

Ok so a bit of an update: I think I found a fix.

I have some new code;

Players.PlayerAdded:Connect(function(p)
	while wait(1) do
		local succ, err = pcall(function()
			MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)
				for i, v in pairs(game.Players:GetPlayers()) do
					if v.UserId == message.Data then
						v:Kick()
					end
				end
			end)
		end)
		if succ then
			print("Success")
		else
			print(err)
		end
	end
end)

Thoughts? [When the function comes, it loops through players to see if it can find a matching user, and when it does, the person is kicked]

2 Likes

That’s probably what I would’ve gone for if I tried to do this. Don’t see any errors, you should be good to go!

1 Like

ye sure it looks great, have you tried it?

Okay, so I ran that code, and there are no errors. But it won’t kick my friend, who is out of studio

Below is all the ban parts to my script

EDIT: SummersFallen is my actual username

local remotes = game:GetService("ReplicatedStorage").AdminRemotes
local DSS = game:GetService("DataStoreService")
local DS1 = DSS:GetDataStore("Banned")

local MessagingService = game:GetService("MessagingService")
local Players = game:GetService("Players")

local ms = game:GetService("MessagingService")

local MESSAGING_TOPIC = "BanServerEvent"

local admins = {"SummersFallen"}



remotes.Ban.OnServerEvent:Connect(function(sender, banned, reason)
	local plr = game.Players:GetPlayerByUserId(banned)

	local plr2 = game.Players:GetNameFromUserIdAsync(banned)

	for i, v in pairs(admins) do
		if sender.Name == v then
			if plr2 == v then
				warn("Player is admin, cannot ban")
			else
				local user = Instance.new("StringValue")
				user.Name = "user"
				user.Parent = game.ServerStorage:WaitForChild("Bans")
				user.Value = tostring(banned)

				DS1:SetAsync(banned,user.Value)

				if game.Players:FindFirstChild(plr2) then
					plr:Kick(reason)
					game.ReplicatedStorage.OtherRemotes.Banned:FireAllClients(plr2)
					
					local publishSuccess, publishResult = pcall(function()
						MessagingService:PublishAsync(MESSAGING_TOPIC, tostring(plr2))
					end)
					if not publishSuccess then
						print(publishResult)
					end
				else
					game.ReplicatedStorage.OtherRemotes.Banned:FireAllClients(plr2)
					
					local publishSuccess, publishResult = pcall(function()
						MessagingService:PublishAsync(MESSAGING_TOPIC, tostring(plr2))
					end)
					if not publishSuccess then
						print(publishResult)
					end
				end
			end
		else
			warn("Player requesting user ban is not an admin!")
		end
	end
end)

game.Players.PlayerAdded:Connect(function(plr)

	local data = DS1:GetAsync(plr.UserId)

	if data then
		plr:Kick("Previously Banned")
	else
		print("Not Banned")
	end
end)

remotes.Kick.OnServerEvent:Connect(function(sender, kicked, reason)
	local plr = game.Players:GetPlayerByUserId(kicked)

	for i, v in pairs(admins) do
		if sender.Name == v then
			if plr.Name == v then
				warn("Player is admin, cannot kick")
			else
				if reason.Text ~= nil then
					plr:Kick(reason)
				else
					plr:Kick()
				end
			end
		else
			warn("Player requesting user kick is not admin")
		end
	end
end)

Players.PlayerAdded:Connect(function(p)
		local succ, err = pcall(function()
			MessagingService:SubscribeAsync(MESSAGING_TOPIC, function(message)
				for i, v in pairs(game.Players:GetPlayers()) do
					if v.UserId == message.Data then
						v:Kick()
					end
				end
			end)
		end)
	if succ then
		print("Success")
	else
		print(err)
	end
end)
1 Like

Hi, sorry for the extremely late reply, you need to both be in a roblox game in order for it to work I think.