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

What is the problem in this script?

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

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

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.

1 comment

0

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

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