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)
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.
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)
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)
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.