I am making a button that whenever a player touches it with any of the both legs the button will go down 2 studs to give it a little effect, but I don't know where the problem is!
There's an activator part which has the script in it and it will cause the button go down 2 studs
1 | part = script.Parent |
2 | button = script.Parent.Parent.Button.Button |
3 |
4 | part.Touched:connect( function (hit) |
5 | if hit.Name = = "Left Leg" or "Right Leg" then |
6 | button.Position.Y = button.Position.Y - 2 |
7 | end |
8 | end ) |
GeezuzFusion's answer is correct.
There is another problem with your code.
hit.Name == "Left Leg" or "Right Leg"
does not mean what you think it does.
The actual order of operations is this: (hit.Name == "Left Leg") or "Right Leg"
You compare the name to "Left Leg", and otherwise get "Right Leg". That means you'll always proceed.
You must explicitly make each comparison:
1 | if hit.Name = = "Left Leg" or hit.Name = = "Right Leg" then |
You can't Assign and process arithmetic values to a variable individually (ex: X, Y, Z). You have to use a Vector3 value or a CFrame Value.
1 | part = script.Parent |
2 | button = script.Parent -- I Changed this to test it. |
3 |
4 | part.Touched:connect( function (hit) |
5 | local x, y, z = button.Position.X, button.Position.Y, button.Position.Z -- Assign Each Value |
6 | if hit.Name = = "Left Leg" or "Right Leg" then |
7 | button.CFrame = CFrame.new(x, y - 2 ,z) -- This is how you should apply arithmetic. |
8 | end |
9 | end ) |
This should work.