Door opener script works until it breaks

I have a door opener tool in my game that works until it breaks and then it won’t work again, when you spam click the door it opens and then closes like 2 times and then breaks. Telling players not to spam click it for it to not break would work just not for long as there’s always gonna be someone trying to find out what it does and then the door breaks which makes it impossible to get into the building.

The Server script in the door itself:

ref=script.Parent.ref
ref.Changed:connect(function()
	if ref.Value==nil then return end
	if ref.Value.Parent==nil then ref.Value=nil return end
	script.Parent.door.sign.SurfaceGui.TextLabel.Text=ref.Value.Name
end)
local door_debounce=false
function setDoorState(state)
	if door_debounce then repeat wait(math.random(1,10)/10) until not door_debounce end door_debounce=true
	if script.Parent.doorstate.Value==state then return end
	script.Parent.doorstate.Value=state
	if state==true then --Open
	for i=1, 10 do wait()
		for _,p in pairs(script.Parent.door:children()) do
			p.CFrame=p.CFrame*CFrame.new(0,0,-.02)
		end
	end wait(0.5)
	for i=1, 40 do wait()
		for _,p in pairs(script.Parent.door:children()) do
			p.CFrame=p.CFrame*CFrame.new(-.1,0,0)
		end
	end
	else --Close
		for i=1, 40 do wait()
			for _,p in pairs(script.Parent.door:children()) do
				p.CFrame=p.CFrame*CFrame.new(.1,0,0)
			end
		end wait(0.5)
		for i=1, 10 do wait()
			for _,p in pairs(script.Parent.door:children()) do
				p.CFrame=p.CFrame*CFrame.new(0,0,.02)
			end
		end
	end
	door_debounce=false
end

script.Parent:WaitForChild("DoorOpenEvent").OnServerInvoke = function()
	script.Parent.action_door.Value=not script.Parent.doorstate.Value
	setDoorState(script.Parent.action_door.Value)
end

The local script inside the tool:

script.Parent.Selected:connect(function(mouse)
	mouse.Button1Down:connect(function()
		local t=mouse.Target
		if t==nil then return end
		if t.Parent==nil then return end
		if t.Parent.Parent:FindFirstChild("action_door") then
			if game.Players.LocalPlayer:DistanceFromCharacter(t.Position)<10 then
			t.Parent.Parent:WaitForChild("DoorOpenEvent"):InvokeServer()
			end
		end
	end)
end)
2 Likes

You can make a cooldown or what some people call a “debounce”

local CD = false
script.Parent.Selected:connect(function(mouse)
	mouse.Button1Down:connect(function()
if CD == true then return end
CD = true 
coroutine.wrap(function()
wait(0.2)
CD = false
end)()
		local t=mouse.Target
		if t==nil then return end
		if t.Parent==nil then return end
		if t.Parent.Parent:FindFirstChild("action_door") then
			if game.Players.LocalPlayer:DistanceFromCharacter(t.Position)<10 then
			t.Parent.Parent:WaitForChild("DoorOpenEvent"):InvokeServer()
			end
		end
	end)

its getting a ton of red line errors
image

You just need to add another end) at the bottom of the end of the script to fix the red lines.

1 Like

There you go this is a fixed and cleaner version

local CD = false
script.Parent.Selected:connect(function(mouse)
	mouse.Button1Down:connect(function()

	
		local t= mouse.Target
		if t==nil then return end
		if t.Parent==nil then return end
	
		if t.Parent.Parent:FindFirstChild("action_door") then
			if game.Players.LocalPlayer:DistanceFromCharacter(t.Position)<10 and CD == false then
	CD = true 
		coroutine.wrap(function()
			wait(1)
			CD = false
		end)()
				t.Parent.Parent:WaitForChild("DoorOpenEvent"):InvokeServer()
			end

		end
	end)
end)

The door still breaks like it use to

Add more time between the wait()
like 1 to 2 seconds

wait(1)
CD = false

I tried that right up to 5 and still issue, Is this time thing suppose to make it so you can only open/close every that amount of time because if it is then its not working.

That’s what it’s suppose to do, I have edited the script above, so hopefully it should work now

Just tried it out and it broke after 1 time

Do you get any error messages in the Output Window when testing it?

No, I don’t get any errors. When the door breaks it does not work again even If I try and reset.

Just put print statements like print("function 1") or print("called DoorOpenEvent") in each section and see which is the last one that prints.

Do I put the print command in both scripts at the top?

In each section where an if statement is. That way you’ll be able to see which if statements are working and which ones aren’t.

Also why do you have if door_debounce then repeat wait(math.random(1,10)/10) in the first script?
Basically it’s making your debounce a random amount of time between .1 and 1 second until door_debounce = false. That seems really strange in a debounce system.

Wait a sec, if door_debounce is false then you’re saying in that line "if door_debounce is true then wait a random amount until door_debounce is false. that’s just never going to work because I don’t see anywhere that you set door_debounce to true.

Another thing, instead of CFraming all the Anchored Parts in the door why not have a door model with an anchored PrimaryPart and have all the other Unanchored Parts of the Model welded to it. That way you can just Tween the door and not have to run through the pairs each and every time.

Sorry for the late response, I just woke up. What would I have to do to fix it, and If I am not able to fix it as I am not very good at scripting how much should I pay someone to fix it for me?

Put print() to everywhere so you can know when it gets broken.

I put the “print()” On top and bottom of both scripts and when I join in studio to test out the door it prints nothing.