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

What is the problem in this script?

Asked by 9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.

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

1part = script.Parent
2button = script.Parent.Parent.Button.Button
3 
4part.Touched:connect(function(hit)
5    if hit.Name == "Left Leg" or "Right Leg" then
6        button.Position.Y = button.Position.Y - 2
7    end
8end)

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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:

1if hit.Name == "Left Leg" or hit.Name == "Right Leg" then
Ad
Log in to vote
-1
Answered by 9 years ago

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.

1part = script.Parent
2button = script.Parent  -- I Changed this to test it.
3 
4part.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
9end)

This should work.

0
button.CFrame = button.CFrame + Vector3.new(0, -2, 0) is a shorter way to say it BlueTaslem 18071 — 9y

Answer this question