ClickDetector only working once

I have a dialogue system with a bug that I can’t make sense of.
So far, the system allows the player to walk away and return, everything works but if you finish the dialogue and try again, the clickdetector doesn’t detect a click.

Here it is working the first time.

Here it is breaking after the first use. (I am spamming my mouse1 button on the npc)

local text = script.Parent.Parent.Head.BillboardGui.Frame.TextBox
local frame = script.Parent.Parent.Head.BillboardGui.Frame
local billboard = script.Parent.Parent.Head.BillboardGui
local cd = script.Parent.Parent.DiaBox.ClickDetector
local plr
local function typewrite(object,text,length)
	for i = 1,#text,1 do 
		object.Text = string.sub(text,1,i)
		wait (length)
	end
end
local dialogueprog = 1
local busy = false
local deb = false
local speechtime = 0.01

local function givetool()
	local clo = game.ServerStorage.Weapons.Dagger:Clone()
	for i, v in pairs (plr.Backpack:GetChildren()) do
		if v:IsA("Tool") then
			v:Destroy()
		end
	end
	for i, v in pairs (clo:GetChildren()) do
		if v:IsA("Script") then
			v.Enabled = true
		end
		if v:IsA("LocalScript") then
			v.Enabled = true
		end
	end
	clo.Parent = plr.Backpack
	script:Destroy()
end

cd.MouseClick:Connect(function(Player)
	print("CD Activated")
	plr = Player
	busy = true
	billboard.Enabled = true
	if dialogueprog == 1 then
		if deb == false then
			deb = true 
			typewrite(text, "Are you sure you want to switch to Dagger Style?", speechtime)
			dialogueprog = 2
			wait(speechtime)
			deb = false
		end
	else
		if dialogueprog == 2 then
			if deb == false then
				deb = true 
				typewrite(text, "Walk away if you do not.", speechtime)
				dialogueprog = 3
				wait(speechtime)
				deb = false
			end
		else
			if dialogueprog == 3 then
				if deb == false then
					deb = true 
					typewrite(text, "Giving Style", speechtime)
					dialogueprog = 1
					givetool()
					wait(speechtime)
					deb = false
					wait (3)
					billboard.Enabled = false
					busy = false
					dialogueprog = 1
				end
			end
		end
	end
end)

while true do
	if busy == true then
		while true do
			local mag = (plr.Character.Torso.Position - script.Parent.Parent.Torso.Position).Magnitude
			if mag<13 then
			else
				billboard.Enabled = false
				busy = false
				dialogueprog = 1
				deb = false
			end 
			wait(.5)
		end
	end
	wait(.5)
end

This is the full script, I am at a complete loss.

1 Like

You are destroying the script at the end of the givetool function.

Oh, I am! It’s a remnant from trying to use it on another script and just copying and pasting it. Thank you!