Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Kick block for if people aren't in a group?

I tried using this code:

script.Parent.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if not player:IsInGroup(5183385) or player:IsInGroup(5183373) then
	player:Kick("Do not enter places you're not supposed to be.")
end
end 

to make a block that kicks the player if they touch it and theyre not in 1 of those 2 groups yet it doesn't work for some reason any ideas why?

Disc: Unknown Intelligence#6569

3 answers

✓ Accepted answer

You were missing a ) at the end of your code. The ids you put in are not groups, one is a model and the other is a hat. I also recommend to check if the part touching the block is actually part of a player's character, otherwise it may break. Also, if you want to check if the player is not in either of the groups, you need to use the and operator, not the or operator unless you want to check if the player is in both groups and when you are checking the second group you didn't put a not before it so it would kick the player if they are in the group.

script.Parent.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        if not player:IsInGroup(5183385) and not player:IsInGroup(5183373) then
            player:Kick("Do not enter places you're not supposed to be.")
        end
    end
end)

This code will fire to any Instance that makes contact with it. If it is touching a baseplate it will essentially try to use workspace to find a Player Object, which obviously won't work. You need to filter for a possible Humanoid Object first before trying to use a theoretical Character Rig, this will ensure the chances of it being an avatar.

local Players = game:GetService("Players")

local Part = script.Parent

Part.Touched:Connect(function(Hit)
	local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid")
	if (Humanoid) then
		local Player = Players:GetPlayerFromCharacter(Hit.Parent)
		if not (Player:IsInGroup(5183385) or Player:IsInGroup(5183373)) then
   			Player:Kick("Do not enter places you're not supposed to be.")
		end
	end
end) --// Missing parentheses.

Ensure this is a ServerScript, as LocalScripts cannot run in global environments such as workspace

Remember to accept this answer if it works!

well the thing is, the not operator evaluates player:IsInGroup(5183385) to the logical inverse of of what it returns.. it doesn't evaluate the second part of the statement, player:IsInGroup(5183373)..

so this leaves the statement to be interpreted as "if the player is not in the first group, but is in the second group, then kick them out".

also, your code needs to check to make sure that GetPlayerFromCharacter returns something.. b/c if the argument passed to it is not a player's character, it returns nil

so rewrite your code like this:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player and not (player:IsInGroup(5183385) or player:IsInGroup(5183373)) then
	    player:Kick("Do not enter places you're not supposed to be.")
	end
end )

This discussion is preserved as it was. New answers, comments, and votes are closed.