So I was looking in roblox forums, and I came across this
Original post Random speed output:
I want to know what this section does, how can character be script.Parent or script.AncestryChanged:Wait()
So I was looking in roblox forums, and I came across this
Original post Random speed output:
I want to know what this section does, how can character be script.Parent or script.AncestryChanged:Wait()
When a script is parented to “StarterCharacterScripts” the script is cloned into the player’s character each time the character reloads/respawns etc.
The “.AncestryChanged” event has two parameters, the child of which had its ancestry changed and the new parent of that child.
wait so the script gets moved when a character respawns? (local script)
also does that bit just set character to whichever value is not nill in script.Parent or script.AncestryChanged:Wait()
The or
operator evaluates the first operand, then if the first operand is falsy, evaluates the second.
Therefore script.Parent or script.AncestryChanged:Wait()
would first check script.Parent
, and if it’s non-nil
, return it. If it is nil
, then it’ll return script.AncestryChanged:Wait()
instead.
Instance.AncestryChanged
fires whenever an Instance
‘s Parent
(or any of its ancestors’ parents) changes. It returns the instance whose Parent
changed, and the new Parent
of the instance.
So the snippet script.Parent or script.AncestryChanged:Wait()
doesn’t actually work as intended and actually resolves to script
if script.Parent
is nil
. Because AncestryChanged
would return script, script.Parent
once its parent gets set, and the second argument would get discarded.
local _, character = nil, script.Parent or script.AncestryChanged:Wait()
print(character.Name)
You’re missing the fact that “_” is declared as the first variable in order to handle the first value (which isn’t needed).
Yields the current thread until this signal is fired. Returns what was fired to the signal.
Wait but shouldn’t script.Parent always return something not nill?
If its nil its been removed. So if you set a variable and remove it, its nil if you try and call it.