If statement doesn't seem to work

This script is supposed to check if they are in the group and then display a decal above their head if they do, as well as having a nametag/rank display.
However, when tested, the decal is shown on everyone who joins the game, even if they aren’t in the group and somehow the if statement isn’t working. I just don’t understand what is not working about it, it seems straightforward to me.

local nameTag= script.NameTag
local flagOffset = 0x1F1E6;
local asciiOffset = 0x41;

local function CreateFlag(part)
	local BillBoard = Instance.new("BillboardGui", part)
	BillBoard.Size = UDim2.new(4, 0, 2, 0)
	BillBoard.SizeOffset = Vector2.new(0, 1.2)
	BillBoard.AlwaysOnTop = true

end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		repeat wait() until character.Head
		
		local plrTag = nameTag:Clone()
		plrTag.PlayerName.Text = player.Name
		plrTag.PlayerRank.Text = player:GetRoleInGroup(6386384)
		
		plrTag.Parent = character.Head
		if player:IsInGroup(6386384) then
			local Flag = CreateFlag(character.Head)
		else
		end

	end)
end)

try this tell me what it prints

local nameTag= script.NameTag
local flagOffset = 0x1F1E6;
local asciiOffset = 0x41;

local function CreateFlag(part)
	local BillBoard = Instance.new("BillboardGui", part)
	BillBoard.Size = UDim2.new(4, 0, 2, 0)
	BillBoard.SizeOffset = Vector2.new(0, 1.2)
	BillBoard.AlwaysOnTop = true

end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		repeat task.wait() until character.Head
		
		local plrTag = nameTag:Clone()
		plrTag.PlayerName.Text = player.Name
		plrTag.PlayerRank.Text = player:GetRoleInGroup(6386384)
		
		plrTag.Parent = character.Head
		if player:IsInGroup(6386384) then
			local Flag = CreateFlag(character.Head)
            print("In group")
		else
            print("Not in group")
		end

	end)
end)

With the provided script, it looks like all it is doing is creating a blank billboard gui

It prints the right things, in group when the player is in the group and not in group when otherwise. The decal of course is still displayed regardless.

Does the thing you’re cloning (nameTag, defined as script.NameTag) already contain the decal? You are cloning the thing before it checks if they are in the group so that may be the issue. The CreateFlag() function is literally only making a blank billboard GUI so you must already have the decal in the NameTag child of the script.

Edit: also using repeat task.wait() until character.Head is an awful way of doing that, just use :WaitForChild() as repeat until statements can bug sometimes

Yes, the decal is inside the billboard GUI called “Nametag.” I have the CreateFlag() function because I frankly don’t know what I’m supposed to put on the other side of local Flag = CreateFlag(character.Head).

How do I remedy cloning it before checking if they are in the group? Simply moving the order of the script makes no difference.

That should make a difference. You need to just put the code starting from where you clone it to where you set the text labels to be inside the if isingroup section.

local nameTag= script.NameTag
local flagOffset = 0x1F1E6;
local asciiOffset = 0x41;

local function CreateFlag(part)
	local BillBoard = Instance.new("BillboardGui", part)
	BillBoard.Size = UDim2.new(4, 0, 2, 0)
	BillBoard.SizeOffset = Vector2.new(0, 1.2)
	BillBoard.AlwaysOnTop = true

end

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		repeat wait() until character.Head
		
	
		if player:IsInGroup(6386384) then
			local Flag = CreateFlag(character.Head)

                        local plrTag = nameTag:Clone()
		plrTag.PlayerName.Text = player.Name
		plrTag.PlayerRank.Text = player:GetRoleInGroup(6386384)
		
		plrTag.Parent = character.Head
		else
		end
	end)
end)

PS. Sorry for bad formatting I am on my phone

Got it, thx for all your help!