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