Using a bindable Event makes tweens not work

I’m making a simple door script. When a player interacts with the door, it will open or close depending on its current state. I’m using bindable events because I have an ai that needs to be able to open the door.

The issue that im currently having is that the bindable events seem to be messing with my tweenclose tween. When i dont use the bindable event, the tweens work just fine.

I have searched google if tweens didn’t work with bindable events, although I came up with nothing.

below ive listed the code that initiates the bindable event

local collectionservice = game:GetService("CollectionService")
local tweenservice = game:GetService("TweenService")


	for i, door in pairs(collectionservice:GetTagged("Door")) do
		
		local hinge = door.Hinge
		local prompt = door.Door.ProximityPrompt

	
		prompt.Triggered:Connect(function()
			game.ServerStorage.Bindables.Door.Event:Fire(door)
		end)
	end


And here is the recieving end of the bindable event.

game.ServerStorage.Bindables.Door.Event.Event:Connect(function(door)
	local TweenService = game:GetService("TweenService")

	local hinge = door.Hinge
	local prompt = door.Door.ProximityPrompt

	local goalOpen = {}
	goalOpen.CFrame = hinge.CFrame * CFrame.Angles(door:GetAttribute("Angle"), math.rad(0), 0)

	local goalClose = {}
	goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)

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


	if prompt.ActionText == "Close" then
		door:SetAttribute("Open", false)
		door.Sounds.DoorClose:Play()
		tweenClose:Play()
		prompt.ActionText = "Open"
	else
		door:SetAttribute("Open", true)
		door.Sounds.DoorOpen:Play()
		tweenOpen:Play()
		prompt.ActionText = "Close"
	end

end)

I will also include the generic script that works, but doesnt support my main goal

local collectionservice = game:GetService("CollectionService")
local tweenservice = game:GetService("TweenService")


for i, door in pairs(collectionservice:GetTagged("Door")) do
	local TweenService = game:GetService("TweenService")

	local hinge = door.Hinge
	local prompt = door.Door.ProximityPrompt

	local goalOpen = {}
	goalOpen.CFrame = hinge.CFrame * CFrame.Angles(door:GetAttribute("Angle"), math.rad(0), 0)

	local goalClose = {}
	goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)

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

	prompt.Triggered:Connect(function()
		if prompt.ActionText == "Close" then
			print("opened")
			door:SetAttribute("Open", false)
			print("opened 2")
			door.Sounds.DoorClose:Play()
			tweenClose:Play()
			prompt.ActionText = "Open"
		else
			door:SetAttribute("Open", true)
			door.Sounds.DoorOpen:Play()
			tweenOpen:Play()
			prompt.ActionText = "Close"
		end
	end)
end

Issue might be related to how the TweenService and BindableEvent are interacting in your code. Since the issue occurs when you introduce the BindableEvent, it’s possible that the event is being fired too quickly causing conflicts with the tweens.

You can modify your code to ensure that the tweens are played only after the BindableEvent has finished processing. One way to achieve this is by wrapping the tween code inside a function and then connecting that function to the BindableEvent.

local function handleDoorInteraction(door)
    local TweenService = game:GetService("TweenService")

    local hinge = door.Hinge
    local prompt = door.Door.ProximityPrompt

    local goalOpen = {}
    goalOpen.CFrame = hinge.CFrame * CFrame.Angles(door:GetAttribute("Angle"), math.rad(0), 0)

    local goalClose = {}
    goalClose.CFrame = hinge.CFrame * CFrame.Angles(0, 0, 0)

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

    if prompt.ActionText == "Close" then
        door:SetAttribute("Open", false)
        door.Sounds.DoorClose:Play()
        tweenClose:Play()
        prompt.ActionText = "Open"
    else
        door:SetAttribute("Open", true)
        door.Sounds.DoorOpen:Play()
        tweenOpen:Play()
        prompt.ActionText = "Close"
    end
end

game.ServerStorage.Bindables.Door.Event.Event:Connect(handleDoorInteraction)

for i, door in pairs(collectionservice:GetTagged("Door")) do
    local prompt = door.Door.ProximityPrompt

    prompt.Triggered:Connect(function()
        game.ServerStorage.Bindables.Door.Event:Fire(door)
    end)
end

I still get the same problem, should I report this as an engine bug?

The bug happens because you’re using CFrame.Angles() wrong in your goalClose CFrame.

Multiplying a CFrame by CFrame.Angles() updates its rotation relatively to its current rotation, meaning if the hinge’s current rotation is 0, 0, 100 then doing hinge.CFrame * CFrame.Angles(0,0,30) will cause the new rotation to be 0, 0, 130.

It doesn’t happen in your non-bindables code because you only set goalClose once in it, i.e. you cache the original rotation of the hinge and reuse it.

You can fix this by changing that line to something like

goalClose.CFrame = CFrame.new(hinge.Position)

which will reset the rotation.

1 Like

well now the door is moving, but it turns sideways when it closes

robloxapp-20240225-0933350.wmv (1.7 MB)

actually, I fixed it, here is the finished code:

local function handleDoorInteraction(door,hingepos)
	local TweenService = game:GetService("TweenService")

	local hinge = door.Hinge
	local prompt = door.Door.ProximityPrompt

	local goalOpen = {}
	goalOpen.CFrame = hinge.CFrame * CFrame.Angles(door:GetAttribute("Angle"), math.rad(0), 0)

	local goalClose = {}
	goalClose.CFrame = hingepos

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

	if prompt.ActionText == "Close" then
		
		door.Sounds.DoorClose:Play()
		tweenClose:Play()
		prompt.ActionText = "Open"
	else
	
		door.Sounds.DoorOpen:Play()
		tweenOpen:Play()
		prompt.ActionText = "Close"
	end
end

game.ServerStorage.Bindables.Door.Event.Event:Connect(handleDoorInteraction)

for i, door in pairs(game:GetService("CollectionService"):GetTagged("Door")) do
	local prompt = door.Door.ProximityPrompt
	local hinge = door.Hinge
	local hingecframe = hinge.CFrame
	prompt.Triggered:Connect(function()
		
		game.ServerStorage.Bindables.Door.Event:Fire(door, hingecframe)
	end)
end

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