[HELP] Admin Panel Kick Function

  1. The idea of what I want to happen is that once a player is chosen from a list, and the kick button is pressed, the player gets removed from the game.

  2. The issue is, I am keeping track of everything through UserId and not Player. With this, I can not seem to find a way to get the player when actually trying to do the kick function.

  3. The only solution that looked reasonable was to try and do “GetPlayerFromUserId”. I had tried that but it did not work.

local Event = game:GetService("ReplicatedStorage"):WaitForChild("KickEvent")
local SelVal = game:GetService("ReplicatedStorage").SelVal


Event.OnServerEvent:Connect(function(player)
	print("Yes")
	if player.UserId == SelVal.Value then
		print(player.UserId)
		player.UserId:Kick("You have been kicked from the game!")
		print(player.Name .. "Has been removed!")
    end
end)

One thing to note is that the UserId is being stored on a value in ReplicatedStorage, I want to get that number, and be able to kick from it. I want the player to be found from the UserId being stored and then use the kick function from there.

It should be :

player:Kick()

not

player.UserId:Kick()

I believe this is what you’re trying to accomplish???

local Event = game:GetService("ReplicatedStorage"):WaitForChild("KickEvent")
local SelVal = game:GetService("ReplicatedStorage").SelVal


Event.OnServerEvent:Connect(function(plr)
	local player  = game.Players:GetPlayerByUserId(SelVal.Value)
	if player then
		player:Kick("You have been kicked from the game")
	end
end)

You should also add a few sanity checks (You should really make sure a game admin the one firing the event).

Something like this:

local Event = game:GetService("ReplicatedStorage"):WaitForChild("KickEvent")
local SelVal = game:GetService("ReplicatedStorage").SelVal

local admins = {123} -- Enter game admins userIds


local function plrIsAdmin(player)
    return table.find(admins, player.UserId)
end


Event.OnServerEvent:Connect(function(plr)
	if plrIsAdmin(plr) then
		local player  = game.Players:GetPlayerByUserId(SelVal.Value)
		if player then
			player:Kick("You have been kicked from the game")
		end
	end
end)

EDIT: Added @LukaDev_0’s suggestion

plrIsAdmin() can be shortend down to

return table.find(admins, player.UserId)
1 Like

I tried doing what you said and it doesn’t give any errors, but it also doesn’t kick the user.

Did you put your userId (98221754) in the Admins table if you used the second version of my script?

Are you effectively changing SelVal.Value to a userId from a localscript?

1 Like

I didn’t do the second part for now as this is a test project, and yes I am changing the SelVal.Value to a userId from a localscript.

Changing it from a LocalScript won’t make it replicate to the server.