Door Open and Close sound while using TweenService

I’m trying to make a tween door, looked up on youtube and copied the scripts. I added a new line into the script using sounds and when it’s opening the sound bugs, but if it closes it’s completly normal. Here’s my scripts.

Door script:

local Hinge = script.Parent.PrimaryPart
local opened = false

local Promt = script.Parent:WaitForChild("ProximityPrompt")

function OpenDoor()
	if opened == false then
		opened = true
		for i = 1, 21 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(5), 0))
			wait()
		end
	else
		opened = false
		for i = 1, 21 do
			script.Parent:SetPrimaryPartCFrame(Hinge.CFrame*CFrame.Angles(0, math.rad(-5), 0))
			wait()
		end
	end
end
Promt.Triggered:Connect(function(Players)
	OpenDoor()
end)
script.Parent.EntranceDoor.ProximityPrompt.Triggered:Connect(OpenDoor)

Script that makes the sound while opening and closing:

workspace.Part.ProximityPrompt.Triggered:Connect(function(player)
	if script.Parent.Parent.Union.Position == Vector3.new(42.133, 22.513, 24.718) then
		script.Parent.Parent.Union.DoorOpen:Play()
	else if script.Parent.Parent.Union.Position == Vector3.new(38.812, 22.525, 22.304) then
			script.Parent.Parent.Union.DoorClose:Play()
		end
	end
end)

It would be nice if someone can improve one of my scripts, or both of them.

2 Likes

In what way does it bug? Can you describe the situation more clearly?

firstly elseif has to be 1 line, and doesn’t come with an extra end.

workspace.Part.ProximityPrompt.Triggered:Connect(function(player)
    if script.Parent.Parent.Union.Position == Vector3.new(42.133, 22.513, 24.718) then
	    script.Parent.Parent.Union.DoorOpen:Play()
    elseif script.Parent.Parent.Union.Position == Vector3.new(38.812, 22.525, 22.304) then
	    script.Parent.Parent.Union.DoorClose:Play()
    end
end)

As @Lightlimn asks, it would be great to know some specifics about the situation. However, I do see some potential issues with your code.

Note: if the below content is or seems too difficult to understand, skip to the very bottom.

For one thing, why is it that you have both Prompt.Triggered and EntranceDoor.ProximityPrompt.Triggered connections calling OpenDoor()? Shouldn’t one proximity prompt be enough? Having two connections may be calling your function twice.

For another, there may be an issue with your OpenDoor() function in the event that it is called multiple times. I believe it will be called on separate routines when called as a callback function which means that there can be separate instances of it running. This is problematic because of your for loop. If you called OpenDoor() in one routine and then immediately called it again in another, then the following would happen:

  1. On the first call, opened is false, so the first for loop runs. Opened is then set to true, as per your code.
  2. On the second call, because opened is now true, the second for loop is run.
  3. Because both calls were started at very close times, both loops are trying to run at the same time, which means that your door is both trying to open and close.

This issue with for loops is real only if my assumption of the routines is correct. If not, sorry about wasting your time reading that lol.

A third issue that I can see with this code that may be causing you problems is the fact that you have yet another proximity prompt connection along with the logic inside that function. In this connection, you compare the door position with a pre-determined position. However, whenever you need to determine the position or numeric value of something in order to do something else, you must consider whether or not you should use the equals operator or a greater than/less than operator. This is even more important when the numeric value could be a decimal number. In the case of comparing positions, the biggest problem here is that no physics engine is consistent. The position of your door could have an x-position of 42.133, as you check for, but this is not guaranteed. This is especially true with floating-point numbers (decimals) because of inaccuracies in arithmetic with decimals (the inaccuracy exists due to how floating-point numbers are stored in memory and are unavoidable). So what should you do instead? Well, consider using <= or >=. Instead of checking if the position of your door is exactly equal to another position, check if your door has gone past a position.

Finally, are the proximity prompts you’re connecting to all the same proximity prompt? If so, why not just make one connection and put all the logic in the function it connects to? Why separate it? This is inefficient not only to run but also for you to work with. Debugging would be vague because you would need to look at each connection to see where the problem is. Adding new code would also be ambiguous because you could put it in any of the connections. Doing so would then make your code very unorganized, generating a positive feedback loop in which the more unorganized it is, the more unorganized you will program with it, which makes it even more unorganized.


A (hopefully) simplified explanation of what you should look do:

I suggest you remove the sound script and modify your door script’s OpenDoor() function to the following:

...
-- Use variables to store data and access stuff quicker. It's both easier to type and more readable
local DoorOpenSound = ... -- the location of the opening sound
local DoorCloseSound = ... -- the location of the closing sound

function OpenDoor()
    if opened == false then
        opened = true
        -- The door is closed, and should now be opened. Play the opening sound
        DoorOpenSound:Play()
        -- your for loop code
        ...
    else
        opened = false
        -- The door is opened, and should now be closed. Play the closing sound
        DoorCloseSound:Play()
        -- your for loop code
        ...
    end
end

The intent of this change is to do two things: organize your code and (hopefully) make your sound work. The organization comes from the fact that you now have all code that deals with the door in the door script rather than some other script that may not be related. This code should also help your issue because it plays the sound based on whenever the proximity prompt is activated rather than whenever the door is at a certain position. I mean, in real life, when you open a door, the door makes a sound, as opposed to the door thinking “oh hey, I’m at this position, so I’m going to play my creaky opening sound!”

3 Likes

Just a note, it’s not the functioning of the DOOR that is broken, it’s the sound which is in a complete simple script.

1 Like

I know, but when looking at it I see other issues that may cause problems in the future.

True, I guess you are correct.

Sorry for the late response I hope you read it at the time right it isnt solved right now or perhaps it is:

Basically when I opened the door, the door open sound bugs, so that the sound plays multiple times and when closing the sound is too late for that.

I hope I describe my future topics a bit better and thanks for the script all though one question is still remaining: does the

mean that I can insert the scripts I added to my previous ones?

The ellipses (...) merely implies that I’m too lazy to type/copy paste what you already have written. It’s a fill in the blank for you to put in the code you already have at those locations. For example, where I say -- your for loop code above a ... I imply that you just take the for loop you already have and put it there. That’s all.

1 Like

Sorry for the misunderstanding, I’m just a bit new into scriptings and it’s a bit difficult to me that’s why I need help to most of the things.

1 Like