Script Issue: Uniform Giver

Hello! I’m having issues with a script I’m making. This script is designed to give people who join a specific uniform depending on their rank in a group.

The script:
local groupid = 7331168

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(groupid) >= 2 and player:GetRankInGroup(groupid) <= 9 then
		player.Character.Shirt.ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate
		player.Character.Pants.PantsTemplate = game.Workspace.LowRankUniform.Pants.PantsTemplate
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(groupid) >= 10 and player:GetRankInGroup(groupid) <= 18 then
		player.Character.Shirt.ShirtTemplate = game.Workspace.MiddleRankUniform.Shirt.ShirtTemplate
		player.Character.Pants.PantsTemplate = game.Workspace.MiddleRankUniform.Pants.PantsTemplate
	end
end)

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(groupid) >= 19 then
		player.Character.Shirt.ShirtTemplate = game.Workspace.CorpRankUniform.Shirt.ShirtTemplate
		player.Character.Pants.PantsTemplate = game.Workspace.CorpRankUniform.Pants.PantsTemplate
	end
end)

Let me know if you can help.

you don’t wait for the character. when the player joins it takes some time for the character to load. try adding a characted added event inside the player added.

1 Like

Could I do if player:GetRankInGroup(groupid) >= 2 and player:GetRankInGroup(groupid) <= 9 and player.Character then ?

Try this script:

game.Players.PlayerAdded:Connect(function(player)
	if player:GetRankInGroup(groupid) >= 2 and player:GetRankInGroup(groupid) <= 9 then
		player.CharacterAdded:Connect(function()
			player.Character.Shirt.ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate
			player.Character.Pants.PantsTemplate = game.Workspace.LowRankUniform.Pants.PantsTemplate
		end)
	end
	if player:GetRankInGroup(groupid) >= 10 and player:GetRankInGroup(groupid) <= 18 then
		player.CharacterAdded:Connect(function()
			player.Character.Shirt.ShirtTemplate = game.Workspace.MiddleRankUniform.Shirt.ShirtTemplate
			player.Character.Pants.PantsTemplate = game.Workspace.MiddleRankUniform.Pants.PantsTemplate
		end)
	end
	if player:GetRankInGroup(groupid) >= 19 then
		player.CharacterAdded:Connect(function()
			player.Character.Shirt.ShirtTemplate = game.Workspace.CorpRankUniform.Shirt.ShirtTemplate
			player.Character.Pants.PantsTemplate = game.Workspace.CorpRankUniform.Pants.PantsTemplate
		end)
	end
end)
1 Like
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		if player:GetRankInGroup(groupid) >= 2 and player:GetRankInGroup(groupid) <= 9 then
			Character.Shirt.ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate
			Character.Pants.PantsTemplate = game.Workspace.LowRankUniform.Pants.PantsTemplate
		elseif  player:GetRankInGroup(groupid) >= 10 and player:GetRankInGroup(groupid) <= 18 then 
			Character.Shirt.ShirtTemplate = game.Workspace.MiddleRankUniform.Shirt.ShirtTemplate
			Character.Pants.PantsTemplate = game.Workspace.MiddleRankUniform.Pants.PantsTemplate
		elseif player:GetRankInGroup(groupid) >= 19 then 
			Character.Shirt.ShirtTemplate = game.Workspace.CorpRankUniform.Shirt.ShirtTemplate
			Character.Pants.PantsTemplate = game.Workspace.CorpRankUniform.Pants.PantsTemplate
		end
	end)
end)

why set a event listener if you don’t even know if its necessary.

image

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		
		local ShirtTemplate = Character:WaitForChild("Shirt")
		local PantsTemplate = Character:WaitForChild("Pants")
		
		if player:GetRankInGroup(groupid) >= 2 and player:GetRankInGroup(groupid) <= 9 then
			ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate
			PantsTemplate = game.Workspace.LowRankUniform.Pants.PantsTemplate
		elseif  player:GetRankInGroup(groupid) >= 10 and player:GetRankInGroup(groupid) <= 18 then 
			ShirtTemplate = game.Workspace.MiddleRankUniform.Shirt.ShirtTemplate
			PantsTemplate = game.Workspace.MiddleRankUniform.Pants.PantsTemplate
		elseif player:GetRankInGroup(groupid) >= 19 then 
			ShirtTemplate = game.Workspace.CorpRankUniform.Shirt.ShirtTemplate
			PantsTemplate = game.Workspace.CorpRankUniform.Pants.PantsTemplate
		end
	end)
end)

yea I gotta edit that, can’t preview on mobile sadly

There are two problems with this solution. Firstly, when :WaitForChild() does not get a result (the player is not wearing either a shirt or pants), the code will stop and not continue any further. Secondly, by doing ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate, instead of changing the .ShirtTemplate property, you would just set the ShirtTemplate variable.

To prevent these issues, we can simply create the missing clothing. Here is a fixed version:

local groupId = 7331168 -- Group which will be used to check for ranks

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char) -- Wait until the charcter's clothing has been loaded
		local rank = plr:GetRankInGroup(groupId) -- Get the player's rank in the group
		local shirt = char:FindFirstChild("Shirt") or Instance.new("Shirt", char) -- Use the existing shirt OR create a shirt if it does not exist
		local pants = char:FindFirstChild("Pants") or Instance.new("Pants", char) -- Use the existing pants OR create pants if they do not exist
		
		if rank >= 2 and rank <= 9 then
			shirt.ShirtTemplate = game.Workspace.LowRankUniform.Shirt.ShirtTemplate
			pants.PantsTemplate = game.Workspace.LowRankUniform.Pants.PantsTemplate
		elseif rank >= 10 and rank <= 18 then
			shirt.ShirtTemplate = game.Workspace.MiddleRankUniform.Shirt.ShirtTemplate
			pants.PantsTemplate = game.Workspace.MiddleRankUniform.Pants.PantsTemplate
		elseif rank >= 19 then
			shirt.ShirtTemplate = game.Workspace.HighRankUniform.Shirt.ShirtTemplate
			pants.PantsTemplate = game.Workspace.HighRankUniform.Pants.PantsTemplate
		end
	end)
end)
1 Like

Yeah, thanks for rewriting it, I was trying to do it in my bed and it was not working out.

one problem with yours tho

Instance.new(Name, Parent) causes memory leaks

I’ve never heard of that issue, but if it’s such a major or noticeable problem, then I guess you could simply set the parents after the variables have been defined.

yea it’s bad practice, better to define outside the Instance.new

I’m prob not gonna send it now, yea it’s not bad but I don’t know why it didn’t format right when I sent it earlier.

Just to clear up some misconceptions:

As far as I know, this is incorrect. Here is the article Roblox software engineers wrote about it:


It’s better to define the parent after changing properties of a new instance.

1 Like

you do know that is still outside the Instance.new()
you know what I meant

1 Like

Works for every outfit except Corporates.

Never mind, you made an error and I’ve fixed it. Thank you for your help!