1.I want to make a door that closes and opens when the player clicks on it.
2.I tried to tween the CFrame of the door to the CFrames of 2 other parts BUT when I try to open the door I get the following error in the output : “Unable to cast value to Object”.
3.I tried multiple ways of making the door open and close but this one is the closest I got to working.
Down below is the part of the script that doesn’t seem to work.
local door = script.Parent --the door
local DoorCF = door.CFrame
local ClosedP = script.Parent.Parent.ClosedPart
local OpenP = script.Parent.Parent.OpenPart
local CanBeClicked = true
local IsOpen = false
--tween stuff
local tweenS = game:GetService("TweenService")
local TweenInfoStuff = TweenInfo.new(1.5, Enum.EasingStyle.Sine,Enum.EasingDirection.Out,0,false,0.15)
local CloseGoal = {}
CloseGoal.CFrame = ClosedP.CFrame
local OpenGoal = {}
OpenGoal.CFrame = OpenP.CFrame
local CloseTween = tweenS:Create(DoorCF,TweenInfoStuff, CloseGoal) --error happens here
local OpenTween = tweenS:Create(DoorCF,TweenInfoStuff, OpenGoal)
Im not that good at scripting so the script may be a little messy.
local T = game:GetService('TweenService')
function tween(o,t,l,s,d)
s = s or Enum.EasingStyle.Linear
d = d or Enum.EasingDirection.InOut
local i = TweenInfo.new(l,s,d)
return T:Create(o,i,t)
end
local door = script.Parent --the door
local DoorCF = door
local ClosedP = script.Parent.Parent:WaitForChild('ClosedPart')
local OpenP = script.Parent.Parent:WaitForChild('OpenPart')
local CanBeClicked = true
local IsOpen = false
local CloseTween = tween(DoorCF,{CFrame=ClosedP.CFrame},1.5)
local OpenTween = tween(DoorCF,{CFrame=OpenP.CFrame},1.5)
Just as an optional thing, I would recommend using a custom tween function like the one I’ve provided at the top of the script. If you’re making tweens a lot, this function makes it a lot easier to use and remember (plus also has defaults).
Arguments for that specific function are as follows:
object/instance, array of properties/goals, length of time, easing style (default is Linear), and easing direction (default is InOut).
The first argument to TweenService:Create() is not a CFrame but the instance the tween must be created for so you need to pass the door instance as first parameter and you also need to call :Play() to run the tween
You can learn more about TweenService here: TweenService
Ah, I didn’t notice that DoorCF you had as a CFrame instead of the instance. Should be fixed now. I know you’ve got it solved now, but I’d still recommend using a function like that in the future to save time.