Issue with clothing giver

Greetings! This is my first post on the Developer Forum!
I recently made a clothing giver, however, the script sometimes call an error in game.
How this script works, is it removes the players shirt and pants and adds a new shirt and pants when a player touches a part.

Script:
local Shirt = script.Shirt
local Pants = script.Pants
local Debounce = false
script.Parent.Touched:Connect(function(Touched)
if Touched.Parent:FindFirstChild(“Humanoid”) and Debounce == false then
Debounce = true
Touched.Parent.Shirt:Destroy()
Touched.Parent.Pants:Destroy()
local ShirtClone = Shirt:Clone()
ShirtClone.Parent = Touched.Parent
local PantsClone = Pants:Clone()
PantsClone.Parent = Touched.Parent
wait()
Debounce = false
end
end)

When I touch the part, it works perfectly, however, when some other players touches this part, it returns an error stating that the shirt and pants were not in the player’s character. I have reviewed and script, and I do not know why this is happening. Does anyone know any solutions to this?

1 Like

Sometimes player might not have the shir or pants, or they may have different names
So, you just need to a if check:

 --Shirt
   if Touched.Parent:FindFirstChildOfClass("Shirt") then
         Touched.Parent:FindFirstChildOfClass("Shirt"):Destory()

         local ShirtClone = Shirt:Clone()
         ShirtClone.Parent = Touched.Parent
   else
          local ShirtClone = Shirt:Clone()
         ShirtClone.Parent = Touched.Parent
end

  --Pants
   if Touched.Parent:FindFirstChildOfClass("Pants") then
         Touched.Parent:FindFirstChildOfClass("Pants"):Destory()

         local PantsClone = Pants:Clone()
         ShirtClone.Parent = Touched.Parent
   else
          local PantsClone = Pants:Clone()
         ShirtClone.Parent = Touched.Parent
   end

I didn’t tested it but it should work

I updated the script.

local Shirt = script.Shirt
local Pants = script.Pants
local Debounce = false

script.Parent.Touched:Connect(function(Touched)
	if Touched.Parent:FindFirstChild("Humanoid") and Debounce == false then
	    Debounce = true
	    Touched.Parent:FindFirstChildOfClass("Shirt"):Destory()
	    Touched.Parent:FindFirstChildOfClass("Pants"):Destory()
	    local ShirtClone = Shirt:Clone()
	    ShirtClone.Parent = Touched.Parent
	    local PantsClone = Pants:Clone()
	    PantsClone.Parent = Touched.Parent
	    wait()
	    Debounce = false
    end
end)

This still does not work. What is the issue?

umm, idk, try completely the same, as I did

Found the issue.
I misspelled the word, Destroy.

The script is working.
Hopefully this works for all character in game.

local Shirt = script.Shirt
local Pants = script.Pants
local Debounce = false

script.Parent.Touched:Connect(function(Touched)
	if Touched.Parent:FindFirstChild("Humanoid") and Debounce == false then
    	Debounce = true
    	Touched.Parent:FindFirstChildOfClass("Shirt"):Destroy()
    	Touched.Parent:FindFirstChildOfClass("Pants"):Destroy()
    	local ShirtClone = Shirt:Clone()
    	ShirtClone.Parent = Touched.Parent
    	local PantsClone = Pants:Clone()
    	PantsClone.Parent = Touched.Parent
    	wait()
    	Debounce = false
    end
end)