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

Assigning a variable to multiple things???!!???

Asked by 8 years ago

I don't understand how the code below works, I found it on the wiki. I thought you couldn't assign something to multiple things? Can someone please explain this???

local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") --Assigned to three different things???
0
Couldja post the link? funyun 958 — 8y

1 answer

Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
8 years ago

It's not assigning a variable to three different things. Two of these are just checking if other things are there.


A code like this:

if hit.Parent then

just checks to make sure the parts parent is there. The same thing is happening with the other bit of code. Putting the code in like this pretty much is asking the game "does it exist?" The same thing goes with this code:

local humanoid = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")

It's just checking if each part is there or not, and if it is there, it removes it being assigned from the last part, and assigns it to the next part. So, the code would go like this:

  1. humanoid = hit

  2. If hit.Parent exists, then humanoid = hit.Parent, instead of hit

  3. If hit.Parent:FindFirstChild("Humanoid") exists, then humanoid = hit.Parent:FindFirstChild("Humanoid"), instead of hit.Parent


If you want to test this out to see what I mean, try putting this script into a Part, and testing it:

script.Parent.Touched:connect(function(hit)
    local humanoid = hit
    print("Line 2: "..humanoid.Name)
    local humanoid2 = hit and hit.Parent
    print("Line 4: "..humanoid2.Name)
    local humanoid3 = hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid")
    print("Line 6: "..humanoid3.Name)
end)

It should print out each part separately to the output so you can get a visual of what I'm saying.


Anyways, I hope this cleared things up for you a bit, and I hope I helped. If you have any further questions or problems, please leave a comment below :P

0
Thanks! I figured it was something like that! dragonkeeper467 453 — 8y
0
No prob dude, glad I could help! dyler3 1510 — 8y
Ad

Answer this question