Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Problem with making a door close if already open?

Hi i'm new to scripting so i'm sorry if i make stupid mistakes. Anyways i had made a door that turns its transparency to 1, and it's cancollide to false if you step on a certain part, and if you step on another certain brick it will turn the doors transparency to 0, and it's cancollide to false. I learned a little more about if and then but the guy didn't really explain it very well :/ i'm trying to make it so if the door's transparency is already 1 and you walk on the part that turns its transparency to 1, then it will instead turn it's transparency to 0. I also want to do this with the cancollide. Here's the normal working script that make's the door open if you walk over the correct part, same for closing.



Door = game.Workspace.Door -- Varible
Enter = game.Workspace.Enter -- Varible
Exit = game.Workspace.Exit -- Varible

Enter.touched:connect(function()-- If Enter is touched, do the following
	Door.Transparency = 1 -- Sets the part Door 's transparency to 1
	Door.CanCollide = false -- Sets the part Door 's cancollide to false
	 
	end)

Exit.touched:connect(function()-- If Exit is touched, do the following
	Door.Transparency = 0 -- Sets the part Door 's transparency to 0
	Door.CanCollide = true -- Sets the part Door 's cancollide to false
end)

Here's what i thought would work.

Door = game.Workspace.Door -- Varible
Enter = game.Workspace.Enter -- Varible
Exit = game.Workspace.Exit -- Varible

Enter.touched:connect(function()-- If Enter is touched, do the following
	Door.Transparency = 1 -- Sets the part Door 's transparency to 1
	Door.CanCollide = false -- Sets the part Door 's cancollide to false
	if Door.Transparency < 1 then
		Door.Transparency = 0
	if Door.CanCollide < false then
		Door.CanCollide = true
	end	
	end
	 
	end)

Exit.touched:connect(function()-- If Exit is touched, do the following
	Door.Transparency = 0 -- Sets the part Door 's transparency to 0
	Door.CanCollide = true -- Sets the part Door 's cancollide to false
end)

I thought that if the script would check if Door 's transparency was already 1 (meaning the part Enter has already been touched, which would open the door.) it would change it back to 0. Could i get some help? Also again i aplogize of any stupid mistakes i make. Also does anyone know good lua scripting tutorials? Not the one's made by roblox, they just don't seem to explain enough. One last thing, would it be better to put the door on like a timer, so if someone stepped on the part Enter it would open the door then close after an X amount of seconds? If that would be better for a game maybe you could show me how to do that? Or link a video of someone showing how to do that? Thanks :)

2 comments

0

Use Formatting

0

Please use the codeblock format from lua on everything. Post all your code inbetween the ~~~~~'s, or in multiple smaller ones.

1 answer

why not just have it be toggle? such as code like this

local Open = false
local Door = game.Workspace.Door -- Variable
local Enter = game.Workspace.Enter -- Variable
local Exit = game.Workspace.Exit -- Variable
local Debounce = true

function toggle()
	if Debounce then
		Debounce = false
		if Open then
			Open = false
			Door.Transparency = 0 -- Sets the part Door 's transparency to 0
			Door.CanCollide = true -- Sets the part Door 's cancollide to true
		else
			Open = true
			Door.Transparency = 1 -- Sets the part Door 's transparency to 1
			Door.CanCollide = false -- Sets the part Door 's cancollide to false
		end
		wait(.5) --Mess with this if it doesn't close/open right
		Debounce = true
	end
end

Enter.Touched:connect(toggle)

Exit.Touched:connect(toggle)

I made it so that it toggles and that there is a debounce so it doesn't open and close like crazy

5 comments

0

Thank you that's what i wanted. But like i said i'm new to lua, like 3 videos watched into lua and i don't quite understand line's 5-11

0

Accept my question please?

0

Accept your question?

0

Could someone explain to me how the script TheGreenDeveloper made works?

This discussion is preserved as it was. New answers, comments, and votes are closed.