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 8 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


part = script.Parent button = script.Parent.Parent.Button.Button part.Touched:connect(function(hit) if hit.Name == "Left Leg" or "Right Leg" then button.Position.Y = button.Position.Y - 2 end end)

2 answers

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 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:

if hit.Name == "Left Leg" or hit.Name == "Right Leg" then
Ad
Log in to vote
-1
Answered by 8 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.

part = script.Parent
button = script.Parent  -- I Changed this to test it.

part.Touched:connect(function(hit)
    local x, y, z =  button.Position.X, button.Position.Y, button.Position.Z  -- Assign Each Value
    if hit.Name == "Left Leg" or "Right Leg" then
        button.CFrame  =  CFrame.new(x, y - 2 ,z) -- This is how you should apply arithmetic.
    end
end)

This should work.

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

Answer this question