How to make a model tween when a button is clicked

139 Views Asked by At

My model hierarchy looks like this:

1. Core
2. Union
2. (Other parts I want to tween with the door)
2. Button
3. (Inside the button) Click detector
3. (Inside the button) Local script

OR

An image so it's easier to read enter image description here

local button = script.Parent
local parentPart = button.Parent -- Assuming the parent part contains all the parts you want to move together

local startPosition = parentPart.Position -- Set the starting position of the parent part
local endPosition = Vector3.new(0, 5, 0)  -- Replace with the desired end position

local TweenService = game:GetService("TweenService")

local function onButtonClicked()
    local tweenInfo = TweenInfo.new(
        2,             -- Duration of the tween in seconds
        Enum.EasingStyle.Quad, -- Easing style (you can change this to other styles)
        Enum.EasingDirection.Out
    )
    local tween = TweenService:Create(parentPart, tweenInfo, {Position = endPosition})
    tween:Play()
end

button.MouseClick:Connect(onButtonClicked)

I used the previous code and clicked the button. I expected the model to move upwards, to the point I specified.

I clicked it but nothing occured

1

There are 1 best solutions below

1
ivox On

Does your button part contain a click detector?

local clickDetector = script.Parent.ClickDetector
local parentPart = script.Parent

local tweenService = game:GetService('TweenService')

clickDetector.MouseClick:Connect(function(hit)
    if hit.Parent:FindFirstChild('Humanoid') then
        local tween = tweenService:Create(parentPart, TweenInfo.new(), {Position = Vector3.new(0,5,0)})
        tween:Play()
    end
end)