Uniform GUI not working

I am currently making an FE Uniform GUI and it asks and listens through remote events, and even prints the first few messages in the server script.

Whenever I click the TextButton desired it does not do anything except print “loop activated” which is done by the server script, although it doesn’t go any further.

I have tried using different loops or longer methods, but they do not work.

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("UniformChange")

event.OnServerEvent:Connect(function(plr,shirtid,pantsid)
	
	print("server listened")
	
	local char = plr.Character
	for _,obj in pairs(char:GetChildren()) do
		
		print("loop activated")
		if obj:IsA("Shirt") or obj:IsA("Pants") or obj:IsA("ShirtGraphic") then
			
			obj:Destroy()
			print("clothing destroyed")
			
		else
			
			return("no clothing found")
			
		end
		
	end
	
	local shirt = Instance.new("Shirt")
	local pants = Instance.new("Pants")
	
	shirt.ShirtTemplate = "rbxassetid://"..shirtid
	pants.PantsTemplate = "rbxassetid://"..pantsid
	
	shirt.Parent = char
	pants.Parenet = char
	print("clothing added")
	
end)

Since you are iterating through that if statement for every child of the character model, you are bound to encounter instances that you don’t want to make any changes to. You did a great job implementing the if statement, the code will now only run for the desired children, but you forgot to take into account the return statement under the else. As soon as the loop iterates through something that isn’t what you’re looking for, your code will be returned. In other words, the function will close and no more iterations will be ran.

To fix this, get rid of the else statement so that the if statement won’t do anything if false. You’ve also misspelled “Parent” as “Parenet” close to the end of your script.

Happy scripting! :wink:

1 Like

Oh, thank you~ but I don’t really understand, maybe just tell me the specific words that should be changed? Parent will be resaid!

This should do the trick:

local rs = game:GetService("ReplicatedStorage")
local event = rs:WaitForChild("UniformChange")

event.OnServerEvent:Connect(function(plr,shirtid,pantsid)
	
	print("server listened")
	
	local char = plr.Character
	for _,obj in pairs(char:GetChildren()) do
		
		print("loop activated")
		if obj:IsA("Shirt") or obj:IsA("Pants") or obj:IsA("ShirtGraphic") then
			
			obj:Destroy()
			print("clothing destroyed")

                        --The issue was present here
		end
		
	end
	
	local shirt = Instance.new("Shirt")
	local pants = Instance.new("Pants")
	
	shirt.ShirtTemplate = "rbxassetid://"..shirtid
	pants.PantsTemplate = "rbxassetid://"..pantsid
	
	shirt.Parent = char
	pants.Parent = char
	print("clothing added")
	
end)
1 Like

Wait it worked! Well not really, it just removed the clothing now I should probably find a bbetter way to implement the clothing.

1 Like