Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to check if a certain item is the child of an object before initiating a script?

I have a morph I am building, which is supposed to include parts which will change transparency, but I need to write a script which makes sure that there is only one 'chest' model which is child to the character in order for it to work properly. I have tried to write a script which is meant to check if there is already a 'chest' model child to the character before continuing with the rest of the script, and it went something like this:


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)


  • note that the very beginning of the script in regards to 'found' and 'if found == false then' are the parts which are supposed to check if the 'chest' part is child to the character. It hasn't worked for me so for, so what am I doing wrong?

2 comments

0

I am curious where you found out about "pre class" being as it looks like HTML and how you would know whether or not it is compatible with roblox's scripting?

0

Read my comment on Br4veh3art23's answer. @RockerCaleb1234

1 answer

local found = game.Players.LocalPlayer.Character:FindFirstChild("Chest") --If you are looking for the torso part, change this to "Torso"

if found ~= nil then
	print("Hello, World!")
	--Other code needed
end

4 comments

0

Also, what is the pre class thing you are doing? I've never heard of that being done in Lua, so would you mind showing me a wiki page? Thanks

0

Its not Lua, its HTML. The

 tag defines preformatted text, like the code formatting feature on this website. But in this case, he must have thought that was the correct way to format code on this website, or he copied and pasted it from another.

0

Also, the class attribute is mostly used to point to a class in a CSS style sheet.

0

Woah, I actually have no idea where that came from, the pre class thing. Its not in the original script. But anyways, I am still stuck with this problem where instead of being left with a single chest piece, there is non at all.

This discussion is preserved as it was. New answers, comments, and votes are closed.