How can i Detect if player in Group When Player Touch part?

So in short i want to make ‘Group Chest’ reward,And i dont know how to detect if player in group when they touch part. This is my script So Far :

game.Workspace.GroupChest.TriggerPart.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if Player:IsInGroup(7104281) then
         --[[
          Script Here
        ]]--
    end
end)

i am not sure where the problem is,but Output send me this :

a

3 Likes

That’s the right code to see if a player is in a group, the problem is that player isn’t the player because hit.Parent might have been something other than the actual character.

Say their hat touches the part first, then the child of the hat’s handle, is the Accessory (the hat itself) the parent of the hat being the character.

That’s why you get the error “attempt to index nil with ‘IsInGroup’”

3 Likes

Like @MrLonely1221 said, you’re probably defining “Player” as something that isn’t actually a player.

You could add something like

if hit:IsA(“BasePart) then
—stuff
end

to essentially filter out the wacky, tacky, and complicated objects that could’ve hit your chest. Assuming that all the BaseParts in your starter character are direct children of the character itself, this would make it so the parent of “hit” is actually a character.

4 Likes

I Tried To Print(hit.Parent) and look :

a

so whats the problem now?

2 Likes

Can you try printing what Player is so you can see if it is in fact grabbing the player and it isn’t nil?

1 Like

It’s print the same thing like i print Hit.Parent

w

1 Like

Player:IsInGroup() doesn’t exist, instead, I suggest doing

game.Workspace.GroupChest.TriggerPart.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if Player:GetRankInGroup(7104281) > 0 then
         --[[
          Script Here
        ]]--
    end
end)

The key change here being if PlayerGetRankInGroup(7104281) then

You can get more info about it here: Player | Documentation - Roblox Creator Hub

cough cough

It does exist.
The problem, reading the error message, says “attempt to index NIL with ‘IsInGroup’”

This error message means the thing he’s calling :IsInGroup() on, is nil. But it isn’t nil as the print shows.

I always recommend using

hit:FindFirstAncestorWhichIsA(“Model”)

for getting the player’s character from the touching part, as it might be a tool that is touching or something else

1 Like
game.Workspace.GroupChest.TriggerPart.Touched:Connect(function(hit)
	local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
	if Player and Player:IsA("Player") and Player:IsInGroup(7104281) then
    	--> script here
    end
end)
1 Like

I forget this is a function sometimes.

I see, I must’ve missed it in my first glance at the functions list. Anyways, wrapping the whole part the checks if the player’s in the group with if Player then might fix the issue. I always do this myself to avoid this sort of issue and it hasn’t failed me to this day.

1 Like

if Player then
Is always a good line to have, I use it too.

But @Nogalo’s solution should do the trick and if it doesn’t. I don’t know what’s wrong because they’re both printing out the player’s name…

1 Like

Yeah me too, i am so confused where the problem is

I don’t think @Nogalo’s solution fixes anything more than what we’ve gotten to, because the output doesn’t say it didn’t find anything with the player’s name. Speaking of the output, @JavaKingz would you mind doing print(Player.ClassName) to make sure we’re dealing with the player and not the character?

I Tried it,but output kept sending me error

you should try moving the script to serverscriptservice or workspace, and not put it in PlayerStarterScripts

You should try to do print(Player.ClassName) and tell us the results.

1 Like

The problem is that the part is touching other parts when the game starts, so you need to check if the part is a descendant of a player’s character and get the player by doing local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)

To make sure that the variable Player is equal to something we do if Player then, now that we know the variable Player is equal to something, we check if it is a ‘Player’ by doing Player:IsA("Player"), after we did that, we need to check if the Player is in the group and to do that we do Player:IsInGroup(7104281)

(hoped that help you understand it better)

good point, the part might be touching baseplate the whole time thus firing the error all the time