I'm making a tool that creates a platform below the character's legs for them to walk on. I want the effect where the platform's BrickColor and & Material matches that of the surface the player is currently walking on. How can I get the BrickColor & Material of the part touching the character's Legs?
All BaseParts
have an event that is fired when that part comes in contact with another part:
local part = --insert part path here part.Touched
There are two ways of having code running upon the event firing. In RBX.Lua
, most, if not all events, have two useful methods:
This method will basically stop the script until the event in question is fired. It can be useful if you are looking to run the code in question only a couple times. The syntax for it is:
part.Touched:wait() -- yield the script until this part touches another part -- place code here
However, for your case, I am 99.999999% sure this won't be the best. If your part touches a part that is not the desired one, the code will run anyways.
This method is self-explanatory. It will connect something to that event. That something is a function (a function is a code snippet, basically). :Connect()
does not yield. If the event is fired twice, the code will run twice. So:
part.Touched:Connect(OneFunction)
There are two ways to do a connection:
In this way, you will be declaring the function itself when you are connecting it to the event:
part.Touched:Connect(function() -- code here end)
This is best used if you are going to use this code snippet only once in the whole script.
In this way, you will be declaring the function itself before connecting it to the event:
function f() -- code here end part.Touched:Connect(f)
The advantage of this one is that you can connect the same function to different events or even call the function itself, saving some lines:
part.Touched:Connect(f) part1.Touched:Connect(f) part2.Touched:Connect(f)
Some events, when fired, and when there's a function connected to them, will pass values as parameters. Those values can be used by the function to help execute the code. In the case of the Touched
event, it passes the part the other part came in contact with:
part.Touched:Connect(function(otherPart) -- code here -- you can use the "otherPart" variable as the other part. end)
function onContact(otherPart) -- code here -- you can use the "otherPart" variable as the other part. end part.Touched:Connect(onContact)
In this case, you can use otherPart.Material
and otherPart.BrickColor
Hope I helped!
~dav1000999