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

how do you check if a part in the character exists?

I am trying to check if a part in the character exists, but if i do this script below it does not work. Why does this script not work? if player.Character.Glasses1 then print("your mom") end

3 answers

To ensure your part is located, we will design a function that will use a rigorous search, so to speak.

Firstly, we'll allow this function to use :WaitForChild() to account for Client latency. We'll allow a timeout of five seconds—the internal leniency window before a yield-warn—to locate the Instance. If this limit is surpassed while no Instance is found, we'll engage a .ChildAdded signal to listen for new Instances, and to check whether said newly added Instance is the one we were originally looking for.

####LocalScript; StarterCharacterScripts.

--###----------[[SERVICES]]----------###--
local Player = game:GetService("Players")



--###----------[[VARAIBLES]]----------###--
local Character = Player.Character or Player.CharacterAdded:Wait()

local ChildAdded;



--###----------[[FUNCTIONS]]----------###--
local function CheckForPart()
	local Part = Character:WaitForChild("Glasses1", 1)
	if (Part) then
		---------------
		--// Action for when the part is located.
		---------------
		if (ChildAdded) then
			ChildAdded:Disconnect()
		end
	else
		if (not ChildAdded) then
			ChildAdded = Character.ChildAdded:Connect(CheckForPart)
		end
	end
end



--###----------[[LOGIC]]----------###--
CheckForPart()

5 comments

0

what does the semi colon at the end of the child added variable mean

0

The semi-colon has no actual significance to the Lua compiler. It can act as a line-break though. In this case, since I am only declaring a variable, I optionally chose to do this as it signifies that there is nothing left needed on that line.

0

what does the semi colon at the end of the child added variable mean

0

what is the, "if (ChildAdded)" checking for, and why do you put the command, "ChildAdded:Disconnect()" what does it do?

0

This all links to the proper management of the RBXScriptConnection.

You can use a function: ~~~~~~~~~~~~~~~~~ Instance:FindFirstChild("NameHere")


local Players = game:GetService("Players")
Players.Character:FindFirstChild("whatever the name is")

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