Button that moves object

Hello
I want to build a button that once pressed, changes color and moves a specific object, as seen in the images attached.
I have very, very limited scripting knowledge by the way..

(The images are concepts of what I want to happen)

in roblox and most game engines, position is a vector with 3 components made with Vector3.new(x,y,z) where x is sideways, y is up, z is forwards/backwards. You can also use + or - with vectors as you would with numbers.

you can create a color object with Color.new(r,g,b) where r,g,b are values between 0 and 1 that represent how much red, green, and blue is in the color

A simple way to detect clicks on a part is to put a clickdetector in the part, then reference it in the script, then connect a function to be run when the event clickDetector.MouseClick is fired

local clickDetector -- add a path like game.Workspace.Part.ClickDetector
local targetPart --  add path
local buttonPart -- add path

local function onClicked()
  targetPart.Position += Vector3.new(0,5,0) -- add 5 studs upwards to the part position
  buttonPart.Color = Color3.new(0,1,0) -- adjust to get a better shade of green
end

-- clickDetector.MouseClick is an event that fires when it gets clicked
-- then, the event is connected with :Connect to the function onClicked, so that the onClicked code gets run when mouseClick is fired
clickDetector.MouseClick:Connect(onClicked)
3 Likes

It works well, but how would I make it tween so it is smooth and only able to be used once? Thank you for the help by the way.

1 Like

You need to use the TweenService and use the parts position

Check here for tween documentations but ill also help u more if needed

to make a tween, and used only once, we could do this

local clickDetector -- add a path like game.Workspace.Part.ClickDetector
local targetPart --  add path
local buttonPart -- add path

local TweenService = game:GetService("TweenService") -- TweenService

clickDetector.MouseClick:Connect(function()
    local tween = TweenService:Create(targetPart, TweenInfo.new(2), {Position = targetPart.Position + Vector3.new(0, 5, 0)}) -- tween information, TweenInfo.new(2) is the seconds it takes for the tween to complete, in this case its 2s
    tween:Play() -- play the tween
    buttonPart.Color = Color3.fromRGB(255,0,0) -- rgb is easier to know
    clickDetector.MaxActivationDistance = 0 -- sets the MaxActivationDistance to 0 so the player cannot click it. 
end)
1 Like

Thank you for helping, I managed to add the ability to play a sound once it was pressed.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.