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 :
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’”
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.
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
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)
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.
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?
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)