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.
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.
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.
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)