Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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()
0
what does the semi colon at the end of the child added variable mean Oliverplays100 -3 — 4y
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. Ziffixture 6913 — 4y
0
what does the semi colon at the end of the child added variable mean Oliverplays100 -3 — 4y
0
what is the, "if (ChildAdded)" checking for, and why do you put the command, "ChildAdded:Disconnect()" what does it do? Oliverplays100 -3 — 4y
0
This all links to the proper management of the RBXScriptConnection. Ziffixture 6913 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Log in to vote
-1
Answered by 4 years ago
local Players = game:GetService("Players")
Players.Character:FindFirstChild("whatever the name is")

Answer this question