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

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

1 answer

✓ Accepted answer

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

2 comments

0

Thanks! I figured it was something like that!

0

No prob dude, glad I could help!

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