I am designing a morph which players will, like many morphs, touch in order to be morphed. But, in order to avoid messing up another script which is meant to change the transparency of some of the parts, I need to make sure that only one chest model is located in the player's character. So, I wrote up this script in hopes that I could check to see if the player's character already has a chest, before giving one to him/her.
local found = game.Players.Player.Character:FindFirstChild("Chest")
local Morph = script.Parent.Parent["Chest"]
if found == false then
function Check(plr)
local ToReturn = true
return ToReturn
end
function GiveMorph(plr)
coroutine.resume(coroutine.create(function()
if plr.Character:FindFirstChild(Morph.Name) == nil then
if Check(plr) == true then
wait(0.1)
local Cloned = Morph:clone()
Cloned.Parent = plr.Character
for i, v in pairs(Cloned:GetChildren()) do
coroutine.resume(coroutine.create(function()
if v:IsA("BasePart") then
if v.Name ~= "Middle" then
local w = Instance.new("Weld")
w.Part0 = Cloned["Middle"]
w.Part1 = v
w.C0 = w.Part0.CFrame:inverse() * CFrame.new(w.Part1.Position)
w.C1 = w.Part1.CFrame:inverse() * CFrame.new(w.Part1.Position)
w.Parent = w.Part0
else
coroutine.resume(coroutine.create(function()
wait(0.1)
local w = Instance.new("Weld", plr.Character["Torso"])
w.Part0 = plr.Character["Torso"]
w.Part1 = v
w.C0 = CFrame.new(0, 0, 0)
end))
end
end
end))
end
for i, v in pairs(Cloned:GetChildren()) do
coroutine.resume(coroutine.create(function()
if v:IsA("BasePart") then
v.Anchored = false
v.CanCollide = false
end
end))
end
if plr.Character:findFirstChild("Shirt") ~= nil then
plr.Character["Shirt"]:remove()
end
if script.Parent:findFirstChild("Shirt") ~= nil then
script.Parent["Shirt"]:clone().Parent = plr.Character
end
if plr.Character:findFirstChild("Pants") ~= nil then
plr.Character["Pants"]:remove()
end
if script.Parent:findFirstChild("Pants") ~= nil then
script.Parent["Pants"]:clone().Parent = plr.Character
end
for i, v in pairs(plr.Character:GetChildren()) do
coroutine.resume(coroutine.create(function()
if v:IsA("CharacterMesh") then
v:remove()
end
if v:IsA("Hat") then
v:remove()
end
end))
end
end
end
end))
end
function Touch(hit)
coroutine.resume(coroutine.create(function()
if hit.Parent:findFirstChild("Humanoid") ~= nil then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr ~= nil then
GiveMorph(plr)
wait(5)
end
end
end))
end
script.Parent.Touched:connect(Touch)
it hasn't worked so far, and I am hoping that I can be given advice as how to fix it, or if there is a better solution to my problem. Thank you.
