Trying to press e to open drawer

Hi I’m trying to use proximity prompt so that when the player presses e on a drawer, the drawer will slide open (z axis -1 studs). This is my code. I’ve also welded accordingly

local cabinet = game.Workspace:WaitForChild("Cabinet")
local drawerModel = cabinet.Drawer
local hinge = drawerModel:WaitForChild("hinge")
local drawer = drawerModel:WaitForChild("drawer")
local prompt = drawer.ProximityPrompt

if prompt then
	print("prompt was found")
end

local TweenService = game:GetService("TweenService")

if TweenService then
	print(TweenService)
	print("TweenService was found")
end

local goalOpen = {}
goalOpen = hinge.CFrame + Vector3.new(0, 0, -2)
local goalClose =  {}
goalClose = hinge.CFrame + Vector3.new(0, 0, 2)

if goalOpen then
	print(goalOpen)
	print("goalopen was successfully made")
end

local tweenInfo = TweenInfo.new(2) 
local tweenOpen = TweenService:Create(hinge, tweenInfo, goalOpen)
local tweenClose = TweenService:Create(hinge, tweenInfo, goalClose)

prompt.Triggered:Connect(function()
	print("propmt was triggered")
	if prompt.ActionText=="Open" then
		print("drawer opened")
		tweenOpen:Play()
		prompt.ActionText = "Close"
	else
		print("drawer closed")
		tweenClose:Play()
		prompt.ActionText = "Open"
	end
end)

It won’t work though as it says “Unable to cast dictionary” to creating tweenOpen. I believe the issue is my creation of goalOpen and goalClose nad the usage of the CFrame.
I’m new to coding so I tried to do this without a tutorial and I am not sure if I’m on the right track.
Can someone help me please

1 Like

That error is happening because, here:

You’re changing the value of goalOpen and goalClose from a table to a CFrame, rather than creating a key within the table that holds a CFrame value, so to fix this problem, you’ll need to do this instead:

local goalOpen = {}
goalOpen.CFrame = hinge.CFrame + Vector3.new(0, 0, -2)
local goalClose =  {}
goalClose.CFrame = hinge.CFrame + Vector3.new(0, 0, 2)
2 Likes

Thank yo uso much! I did not notice that

1 Like

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