Sounds for door not working

So i made a simple door
Screenshot_67
And i tried to add a sound to it’s opening/closing sequence, but to no avail
here’s code for the door, i would like to know what i did wrong:

 script.Parent.ClickDetector.MouseClick:Connect (function(player)
	if script.Parent.ClickDetector.MouseClick
	then
		script.Parent.CanCollide = (false)
		script.Parent.Transparency = (1)
	
		script.Parent.Open.Played = (true)
		
		game.Workspace.Door2.CanCollide = (true)
		game.Workspace.Door2.Transparency = (0)
		
		wait (3)
		
		if script.Parent.ClickDetector.MouseClick
		then
		script.Parent.CanCollide = (true)
		script.Parent.Transparency = (0)
		script.Parent.Close.Played = (true)
		
		Sound:Play() (function()
		script.Parent.Close.Played = (true)
		
		
			game.Workspace.Door2.CanCollide = (false)
		game.Workspace.Door2.Transparency = (1)	
		end
		
	end
end)

Also, i already have the sounds, but having trouble when it comes to door playing them.
To clear up confusion, Open and Close are the sounds
Screenshot_68

First, you don’t need the if statement: if script.Parent.ClickDetector.MouseClick then because it is already in a MouseClick event I think so. Second, if you want the effect different every time you click, you can just make a boolean value to declare if it is open or not:

local value = false
script.Parent.ClickDetector.MouseClick:Connect (function(player)
	if value == false then
        value = true
		script.Parent.CanCollide = (false)
		script.Parent.Transparency = (1)
	
		script.Parent.Open.Played = (true)
		
		game.Workspace.Door2.CanCollide = (true)
		game.Workspace.Door2.Transparency = (0)	
	elseif value == true then
        value = false
		script.Parent.CanCollide = (true)
		script.Parent.Transparency = (0)
		script.Parent.Close.Played = (true)
		
		Sound:Play() (function()
		script.Parent.Close.Played = (true)
		
		
		game.Workspace.Door2.CanCollide = (false)
		game.Workspace.Door2.Transparency = (1)	
		end
	end
end)
1 Like

This one breaks the script
This is what script analysis says
Error: (12,15) Expected ‘then’ when parsing if statement, got ‘=’

2 Likes

Do value == true then…
30Chara

but then it script analysis outputs this:
Error: (29,2) Expected ‘)’ (to close ‘(’ at line 21), got ‘end’

EDIT: Read post number 15 instead if you have stumbled across this thread in the future! I initially made a mistake in this comment and I rewrote the script (kinda) to match what OP was looking for later on.

In addition to what the others have mentioned, take note that you do not need to add parenthesis around the values that are being changed.

Here’s a modification of the script that has the functionality you intended for it to have, along with turning script.Parent into a variable so it was more efficient and easier to reference. I’ve added comments that will allow you to understand the revisions that were made

local door = script.Parent -- This turns "door" into a variable. Whenever you type "door", it will mean script.Parent
local door2 = game.Workspace.Door2 -- This turns "door2" into a variable. Whenever you type "door2", it will mean game.Workspace.Door2

door.ClickDetector.MouseClick:Connect(function()
		if door.CanCollide == true then -- If the door is closed (CanCollide = true) then...
		
		door.CanCollide = false -- The door opens!
		door.Parent.Transparency = 1
-- Edit: This door.Parent is where I messed up when copy-pasting OP's original script to re-write it.
		local Open = door.Open -- This defines "Open" as the sound effect called "Open" inside of the door
		Open:Play() -- This plays the sound called "Open"
		
		door2.CanCollide = true
		door2.Transparency = 0
end
		if door.CanCollide == false then -- If the door is open (CanCollide = false) then...
		
		door.CanCollide = true -- The door closes!
		door.Transparency = 0
		
		local Close = door.Close -- This defines "Close" as the sound effect called "Close" inside of the door
		Close:Play() -- This plays the sound called "Close"
		
		door2.CanCollide = false
		door2.Transparency = 1	
	end
end)

I wasn’t sure why you had two doors, but I left the values you set for it untouched so you could modify it as needed to fit your scenario. If I made any mistake during this, please correct me! Hope this ends up working for you :slight_smile:

1 Like

The script didn’t seem to work, and there are two doors because i wanted it to have animation, as if the door was actually opened

For making an animated door check Tweening Service it helps.

1 Like

To add to the TweenService, I’d recommend this function that I use:

local T = game:GetService('TweenService')
function tween(o,t,l,s,d)
	if not s then s = Enum.EasingStyle.Linear end
	if not d then d = Enum.EasingDirection.InOut end
	local i = TweenInfo.new(l,s,d)
	return T:Create(o,i,t)
end
1 Like

Oh I made a mistake right there. Thanks for telling me.

why are you attempting to check for whether the MouseClick event fired? It fires when a Click detector is clicked already

why is everything wrapped in brackets? there is no need to do that, it can be

   script.Parent.CanCollide = false -- just that

why create another function? all the code within the function binded to the MouseClick event will run , no need to create another function.


     local door = script.Parent -- keep the script directly parented to the door
     local sound = door.Sound

     door.ClickDetector.MouseClick:Connect(function(player)
          -- assuming a ClickDetector exists
          sound:Play()
          door.CanCollide = false
          door.Transparency = 1
          -- for this to work, door must be a part- not a model
          door.CanCollide = true
          door.Transparency = 0 
         
     end)

note that this is not a complete working code, rather pseudo-code for demonstration purposes

Thanks for the advice, as i’m very new to scripting (this was the first working script i wrote mostly by myself). So keeping that in mind i’m not very familliar with world of roblox scripting, and most of more advanced techniques are nearly impossible for me to pull off by myself, i might not code in the most efficient way, i might make lots of mistakes, since my learning method is through trial & error, but all of the ranting aside, i would most likely continiue to improve, moving on to more and more advanced projects, and every bit of guidance helps me at that.

1 Like

Do take note that the function within the script they provided will automatically change the CanCollide and Transparency values back to its “closed” state, as shown below.

Because there is nothing stopping or delaying the function from changing the door’s properties back in the latter half of the function, you won’t have any time, realistically, to go through the door. I’ve gone back to correct some mistakes I made in my original reply while substituting your original references for variables I created, along with some additional explanations as to how it works:

local door = script.Parent -- This turns "door" into a variable. Whenever you type "door" in this script, it will mean script.Parent.

door.ClickDetector.MouseClick:Connect(function() -- Runs the function when the door is clicked.
		if door.CanCollide == true then -- If the door is closed (CanCollide = true) then...
		
		door.CanCollide = false -- The door opens!
		door.Transparency = 0.75 -- The door becomes almost completely invisible.
-- I would suggest maintaining partial visibility so that players know there's a door and not just a doorframe.
	
		local Open = door.Open -- This defines "Open" as the sound effect called "Open" inside of the door
		Open:Play() -- This plays the sound called "Open"

		elseif door.CanCollide == false then -- If the door was NOT open when someone clicked it (CanCollide = true), then...
		
		door.CanCollide = true -- The door closes!
		door.Transparency = 0 -- The door becomes entirely visible
		
		local Close = door.Close -- This defines "Close" as the sound effect called "Close" inside of the door
		Close:Play() -- This plays the sound called "Close"
	end -- Ends the "if then" statement. Each "if then" statement needs an end.
end) -- Ends the function. Each function needs an end.

This is basically the same thing that I mentioned the other day, but I fixed one part where I put “door.Parent.Transparency”, which didn’t reference the door properly. I also removed door2 because I’ve just rewritten this for the example of a clickable door that opens and closes. However, this could be spammed, so we could add something called “debounce” to the script which will prevent an overflow of inputs. This is something that you will most commonly start to use when utilizing the .Touched event.

local door = script.Parent -- This turns "door" into a variable. Whenever you type "door" in this script, it will mean script.Parent.
local debounce = false -- Makes our imaginary variable called "debounce" set to false. You can name it whatever you want.

door.ClickDetector.MouseClick:Connect(function() -- Runs the function when the door is clicked.
		if debounce == false -- If our variable is set to false, then...
		then debounce = true -- It will be set to true.
			
		if door.CanCollide == true then -- If the door is closed (CanCollide = true) then...
		
		door.CanCollide = false -- The door opens!
		door.Transparency = 0.75 -- The door becomes almost completely invisible.
-- I would suggest maintaining partial visibility so that players know there's a door and not just a doorframe.
	
		local Open = door.Open -- This defines "Open" as the sound effect called "Open" inside of the door
		Open:Play() -- This plays the sound called "Open"

		elseif door.CanCollide == false then -- If the door was NOT open when someone clicked it (CanCollide = true), then...
		
		door.CanCollide = true -- The door closes!
		door.Transparency = 0 -- The door becomes entirely visible
		
		local Close = door.Close -- This defines "Close" as the sound effect called "Close" inside of the door
		Close:Play() -- This plays the sound called "Close"
	end -- Ends the "if then" statement. Each "if then" statement needs an end.
end -- Ends the other "if then" statement. Take note that "elseif"s do not need their own end.
	debounce = false -- Our varible will be set to false to make sure it runs properly the next time it is clicked.
end) -- Ends the function. Each function needs an end.

In this case, “Debounce” is technically imaginary and doesn’t actually have to do anything with the door’s functionality, as the door would still work without it. However, by including debounce, you can greatly reduce the amount of inputs that are sent. If you would like to test this, try using the first script that I provided in this comment and then spam click the door to open and close it. Afterwards, replace it with this second one and you will notice the difference.

Anyway, hope that this has been more useful for you! The trial and error approach can be great in many different instances, and while it won’t help with learning everything, it’s definitely a great strategy that can allow you to further improve upon your development skills and general life skills without having to spend the time in certain instances to look around for an answer. Just remember that it’s good to reference places where it has been answered (when needed) just so you don’t dig yourself into a hole of bad habits! Best of luck on your development journey! :smiley:

I have to make one thing clear as it seems not to be understood:
Door is meant to have a simple animation, and i’m not that skilled to do proper animating yet.
Screenshot_69
Screenshot_70

I’m aware! I was just providing information for you when it came to the usage of one door that could be applicable when you continue to create the other one (as the concept of debounce will be very important to make sure that people can’t spam inputs not just for the door, but other mechanics you implement into your game).

Because tweening has more complexity than the utilization of one door, I wanted to demonstrate how the original code that you showed in the original post could be altered to be more efficient. If you would like to have the alternative two doors from the original post to mimic the animation without having one, you should probably have a ClickDetector in door2 as well, which will make it so people do not have to click the invisible door to change it based to its “Closed” state. Here is what the script would look like if you placed it inside of the original door:

-- This part of the script is for the first door
local door = script.Parent -- This turns "door" into a variable. Whenever you type "door" in this script, it will mean script.Parent
local door2 = game.Workspace.door2
local debounce = false -- Makes our imaginary variable called "debounce" set to false. You can name it whatever you want.

door.ClickDetector.MouseClick:Connect(function() -- Runs the function when the door is clicked.
		if debounce == false -- If our variable is set to false, then...
		then debounce = true -- It will be set to true.
			
		if door.CanCollide == true then -- If the door is closed (CanCollide = true) then...
		
		door.CanCollide = false -- The door opens!
		door.Transparency = 1 -- The door inside of the door frame is completely invisible
	
		local Open = door.Open -- This defines "Open" as the sound effect called "Open" inside of the door
		Open:Play() -- This plays the sound called "Open"
        door2.CanCollide = true
        door2.Transparency = 0
	end -- Ends the "if then" statement. Each "if then" statement needs an end.
end -- Ends the other "if then" statement.
	debounce = false -- Our varible will be set to false to make sure it runs properly the next time it is clicked.
end) -- Ends the function. Each function needs an end.

-- This part of the script is for the second door
local debounce2 = false

door2.ClickDetector.MouseClick:Connect(function() -- Runs the function when door2 is clicked.
		if debounce2 == false -- If our variable is set to false, then...
		then debounce2 = true -- It will be set to true.
			
		if door2.CanCollide == true then -- If door2 is open (CanCollide = true) then...
		
        door2.Transparency = 1 -- Door2 becomes invisible.
		door2.CanCollide = false -- Door2 can no longer be walked through.
		
	
		local Close = door.Close-- This defines "Close" as the sound effect called "Close" inside of the original door
		Close:Play() -- This plays the sound called "Open

		door.CanCollide = true -- The original door can be walked into!
		door.Transparency = 0 -- The original door becomes entirely visible.
	end -- Ends the "if then" statement. Each "if then" statement needs an end.
end -- Ends the other "if then" statement.
	debounce2 = false -- Our varible will be set to false to make sure it runs properly the next time it is clicked.
end) -- Ends the function. Each function needs an end.

I’ve rewritten this completely within this comment editor, so I may have made a mistake (but it is closer to your most recent reply with what you have requested). Make sure you have inserted a ClickDetector into door2 and have made sure that one of the doors has their transparency set to 1 and CanCollide set to false so that you can have the effect work properly.

If you maintained that form (in your original post), it would have been much more difficult and time consuming to implement the door animation with the tween service if there are too many extra lines of code that are not required to allow it to work properly.

jcnruad900 and MightyDantheman have already provided information about the tweening service a few posts up that could assist you in completing the intended door animation. I would suggest familiarizing yourself with code management and possibly writing comments in your own code if it will help you look back through it and understand each step that you want to do. Without this, it could drill in some bad habits that take much longer to reverse in the long run (such as adding parenthesis around values you are changing, adding additional functions that are not needed, etc.)

What I did in my most recent reply to provide an alternative way to understand the script was by making it readable via the comments that is closer to “normal language”, where someone who doesn’t know anything about scripting would understand. Regardless, hope that you’re able to achieve the door animation that you have been looking for along with continuing on your development journey!