Tweenservice direction changes upon parent's object rotation change

So uh, I suck at scripting and I don’t progress a lot cuz I tend to leave it time to time so I’m always stuck in the same knowledge level, but I’ve been trying to learn these days with phind AI but the AI is not very effective and makes scripts that just throw errors many times.

The problem is self-explanatory, basically a button that needs to be pushed forward changes direction when the parent object is rotated which is the button, the button is just part of a soda dispenser, so if I rotate the model obviously everything including the button is gonna rotate and the script works but it’s still following the same direction, and doesn’t change depending in parent object’s orientation

AND I HAVE NO IDEA HOW TO FIX THIS, so does anyone have a better solution than tweenservice or something? and yes I know the soda is in serverstorage instead of being inside the soda dispenser itself which is simpler for public uploads, but I chose that way.

script:

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

-- Reference to the invisible part
local invisiblePart = script.Parent.Parent.spawn

-- Reference to the soda in ServerStorage
local soda = ServerStorage:WaitForChild("BloxyCola")

-- Debounce timer
local debounceTime = 0.5 -- Adjust this value as needed
local lastClickTime = 0

-- Function to spawn the soda
local function spawnSoda()
	local newSoda = soda:Clone()
	newSoda.Handle.Position = invisiblePart.Position
	newSoda.Parent = workspace

	-- Play sound effect
	local buttonSound = script.Parent.Sound
	if buttonSound then
		buttonSound:Play()
	end
end

-- Function to toggle button state
local function toggleButtonState()
	local button = script.Parent

	-- Check if the button has a Size property
	if typeof(button.Size) == "Vector3" then
		local originalPosition = button.Position

		-- Animate button push forward
		local tweenInfo = TweenInfo.new(
			0.3, -- Duration
			Enum.EasingStyle.Sine,
			Enum.EasingDirection.Out
		)
		local tween = TweenService:Create(button, tweenInfo, {Position = Vector3.new(originalPosition.X + 0.1, originalPosition.Y, originalPosition.Z )})

		tween:Play()

		-- Wait for the animation to finish
		tween.Completed:Connect(function()
			-- Animate button back to original position
			local revertTween = TweenService:Create(button, tweenInfo, {Position = originalPosition})
			revertTween:Play()

			spawnSoda() -- Spawn soda after animations complete

			-- Change color of Signal part and PointLight
			local signalPart = script.Parent.Parent.Signal
			local pointLight = signalPart.PointLight

			if signalPart then
				local originalColor = signalPart.Color

				-- Animate color change
				local colorTween = TweenService:Create(signalPart, tweenInfo, {Color = Color3.new(0, 1, 0)})
				colorTween:Play()

				-- Wait for the animation to finish
				colorTween.Completed:Connect(function()
					-- Animate color back to original
					local revertColorTween = TweenService:Create(signalPart, tweenInfo, {Color = originalColor})
					revertColorTween:Play()
				end)	
			end

			if pointLight then
				local originalColor = pointLight.Color
				local originalBrightness = pointLight.Brightness

				-- Animate color change
				local colorTween = TweenService:Create(pointLight, tweenInfo, {Color = Color3.new(1, 1, 0)})
				colorTween:Play()

				-- Wait for the animation to finish
				colorTween.Completed:Connect(function()
					-- Animate color back to original
					local revertColorTween = TweenService:Create(pointLight, tweenInfo, {Color = originalColor})
					revertColorTween:Play()

					-- Animate brightness change
					local intTween = TweenService:Create(pointLight, tweenInfo, {Brightness = 2}) -- Increase brightness temporarily
					intTween:Play()

					-- Wait for the animation to finish
					intTween.Completed:Connect(function()
						-- Animate brightness back to original
						local revertIntTween = TweenService:Create(pointLight, tweenInfo, {Brightness = originalBrightness})
						revertIntTween:Play()
					end)
				end)
			end
		end)
	end
end

-- Find the ClickDetector on the button
local clickDetector = script.Parent:FindFirstChild("ClickDetector")
	clickDetector.MouseClick:Connect(function()
		local currentTime = tick()
		if currentTime - lastClickTime > debounceTime then
			lastClickTime = currentTime
			toggleButtonState()
		end
	end)

anyone pls, cuz I seriously don’t freaking know how to fix this.

Can you send a video of it happening?

My guess is that you are using WeldConstraint which perhaps make the button relative to the parent part

they all have weld yes, you think it could be that??

I deleted the welds, but the issue is still the same, it has to be in the tweenservice code.

Hi! Vector3 in tween is oriented to world, and tween playing relative to world.
I think use Motor6D is the easiest solution.

And also you don’t need use separate reverse tween, you can just specify reverse in TweenInfo

Motor6D is relative to parent?

Uhh i don’t know how to explain it properly. Basically it welds one part to another and have 2 CFrame options: Relative to part0 and relative to part1
image
All CFrames in local space

I might be confused but I don’t seem to know whats the issue here? Everything seems fine

the button is supposed to be pushed forward independently or parent object rotation, but if you rotate the machine it will go right or left or out.

so it is relative to objects I guess?

what u could do is when u spawn the can, parent it out of the model or folder or whatever u have it in, and apply impulse to the lookVector of the machine

Yes, part0 is the zero point of CFrame

Alright I will try it, but do you know how that needs to be done?

Here a model for example
Vending Machine.rbxm (4.6 KB)