Alright, based off of that, I believe that the MouseClick stops working after 25, which is because it gets replaced and removed, causing the function to no longer have any effect. Instead of naming the new ClickDetector “ClickTwo” just replace the Click variable with it and make a new MouseClick function when it is made.
local Door = game.Workspace.Door
local Click = Instance.new("ClickDetector")
Click.Parent = game.Workspace.ClickPart.MainClick
local TextLabel = script.Parent.SurfaceGui.TextLabel
local debounce = false
local connection
function MouseClick()
if not debounce then
debounce = true
TextLabel.Text = tostring(tonumber(TextLabel.Text) + 1)
wait(.5)
debounce = false
end
end
connection = Click.MouseClick:Connect(MouseClick)
if TextLabel.Text == "25" then
TextLabel.Text = "0"
connection:Disconnect()
Click:Destroy()
Door.Transparency = 1
Door.CanCollide = false
wait(5)
Click = Instance.new("ClickDetector")
Click.Parent = game.Workspace.ClickPart.MainClick
connection = Click.MouseClick:Connect(MouseClick)
Door.Transparency = 0
Door.CanCollide = true
end
Gonna ignore the fact you’re using TextLabel.Text, a string, to count…
The if-block that follows your event connection runs only once when the script runs. I’m assuming you want the stuff within that block to run when the count reaches 25, and to accomplish that you need to move that entire if-block into the connected function () ... end.
You’ll also need to provide more context about this script - what’s it supposed to do? We can only assume so much before things become too speculative to be useful.
It’s not a mistake that Lua actually interprets number-like strings as numbers, although it’s highly recommend you never rely on this. As in print("1337" + "3") actually does print 1340, a number. You should be using an actual number to store this and making your TextLabel.Text reflect that.
well when the surface uis get text label get reaches 25 a part called door will become can collide false and invisible. Then after five seconds everything will be restored back to its original state.
Then in that case, move the if-block so it runs in your connected function. Otherwise, if it’s at the end of your code, it only ever runs once. I’ve rewritten your code a bit, and added a few functions which should help make the door logic more clear:
local Door = game.Workspace.Door
local Click = Instance.new("ClickDetector")
Click.Parent = game.Workspace.ClickPart.MainClick
local TextLabel = script.Parent.SurfaceGui.TextLabel
local debounce = false
local count = 0
local function setCount(newCount)
count = newCount
TextLabel.Text = count
end
local function setDoorOpen(open)
if open then
Door.Transparency = 1
Door.CanCollide = false
else
Door.Transparency = 0
Door.CanCollide = true
end
end
setDoorOpen(false)
Click.MouseClick:Connect(function()
if not debounce then
debounce = true
setCount(count + 1)
if count >= 25 then
setCount(0)
setDoorOpen(true)
wait(5)
setDoorOpen(false)
else
wait(.5)
end
debounce = false
end
end)
First one had a typo - fixed it. I meant count = newCount. Both newCount and open represent parameters of the functions setCount and setDoorOpen.
When you call a function, you provide it an argument. For setDoorOpen(true), the true is the argument. In the function definition, open is the parameter which represents the value sent to the function when it was called. I defined two functions which unify the logic of setting the current count value (setCount) and opening the door (setDoorOpen), each of which accept a value.
String type properties will automatically convert numbers so no need to tostring it here, but let’s avoid recommending to OP to use the Text property to store the click count.