Kick someone from the game if they are in a area that is restricted to a rank in a group

hi there i am making a staff only area is there any way i can make this script kick people out of the game if they come in

local player = game:GetService("Players")
function onTouch(hit)
	if game.Players:FindFirstChild(hit.Parent.Name) ~= nil then
		local char = game.Players:FindFirstChild(hit.Parent.Name)
		local name = char
		char:Kick("You have been kicked for entering a Staff Area.")
		script.Parent.Touched:Connect(onTouch)
2 Likes

this should work,

local peopleWhoCanEnter = {'name1', 'name2', 'name3'} -- names of the people who can enter the room

function onTouch(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then -- checks if it's a player
if not peopleWhoCanEnter[hit.Parent.Name] then -- if the player doesn't have access it kicks them
game.Players[hit.Parent.Name]:Kick("You have been kicked for entering a Staff Area.")
end
end
end

script.Parent.Touched:Connect(onTouch)```
2 Likes

is it possible to do a rank in a group?

1 Like

Yes, you’d do this
if game.Players[hit.Parent.Name]:GetRankInGroup(groupId) < rankNumber then
instead of
if not peopleWhoCanEnter[hit.Parent.Name] then

1 Like

so will that work with higher then 4+

1 Like

Didn’t test it out but it should.

1 Like

GetRoleInGroup Returns a string of the current role the person is in in the group or Guest if they aren’t in the group, the proper method is GetRankInGroup.

Also, game.Players:FindFirstChild(hit.Parent.Name) would better of being as game:GetService("Players"):GetPlayerFromCharacter(hit.Parent) and storing it in a variable since you use it more than once

This would be a bettter approach if you just want the door’s purpose to be kicking low ranked people

local allowedRank = 3

local function onTouch(hit)
	local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if not player or player:GetRankInGroup(GROUPID) <= allowedRank then
		return
	end
	player:Kick("You have been kicked for entering a Staff Area.")
end

script.Parent.Touched:Connect(onTouch)

Where GROUPID is the id of the group to check

1 Like

ah thanks so i can do < 3 aka grater then 3 and it shall let higher people pass too

1 Like

Oh right, my mistake, use > instead of <= if you don’t want to allow the rank with the number3 either, otherwise if you want to allow the rank with the number 3 in, use >=

Glad to be of help! @ThattSav

1 Like

Thanks for the help embat :smiley:

2 Likes

Do not use on touch!!! Instead you can use the Zone+ module to create that

1 Like
local players = game:GetService('Players')
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = players:GetPlayerFromCharacter(hit.Parent)
        if not player:GetRankInGroup(--[[group id here]]) >= 4 then
            player:Kick('Message here')
        end
    end
end)

Thanks for being early… lol this thread is 3 years old mate

2 Likes