Dialogue Just skip after 1st time

I dont know why, but my dialogue script breaks after working perfectly fine for 1st time
It skips to the end for no reason

Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local HRP = Character:FindFirstChild("HumanoidRootPart")

local Camera = workspace.CurrentCamera

local Interaction = script.Parent
local GUI = Interaction.Parent

local QuestGui = script.QuestGui
local MainBox = QuestGui["Main Box"]
local NevermindButton = QuestGui["Nevermind Button"]
local YesButton = QuestGui["Yes Button"]
local SkipButton = QuestGui["Skip Button"]

local NPCs = game.ReplicatedStorage.NPCs

local Action = 0

Interaction.Event:Connect(function(NPC, Amount)
	Character:SetAttribute("AbleForInteractions", false)
	
	MainBox.Parent = GUI
	NevermindButton.Parent = GUI
	YesButton.Parent = GUI
	
	MainBox.Text.Text = "Hi there, I have a job for you to complete. Are you interested?"
	Action = 1
	print(Action)
	
	YesButton.HitBox.MouseButton1Click:Connect(function()
		if Action == 1 then
			MainBox.Text.Text = "If you still haven't changed your mind, I will need you to defeat "..tostring(Amount).." "..tostring(NPC).."."
			Action = 2
			print(Action)
		elseif Action == 2 then
			MainBox.Text.Text = "Good Luck."
			
			NevermindButton.Parent = QuestGui
			YesButton.Parent = QuestGui
			
			task.spawn(function()
				task.wait(2)
				MainBox.Parent = QuestGui
				Character:SetAttribute("AbleForInteractions", true)
			end)
			Action = 0
			print(Action)
		end
	end)
end)

Video of what i mean:

1 Like
if Action == 1 then
MainBox.Text.Text =("If you still haven't changed your mind, I will need you to defeat "..tostring(Amount).." "..tostring(NPC)..".")
task.wait()
Action = 2
print(Action)

Try adding the task.Wait() and the () around the MainBoxText Var

I believe I see why you’re having this problem. So it works perfectly fine the first time because you connected the event and it does as it’s supposed to, but the second time you call the “interaction” event you’re created a second MouseButton1Click event which upon clicking the button will simultaneously call 2 events which affects your “actions” variable.

You can fix this by putting the function outside of the connection and making it a variable, then when you connect it put that variable name inside.

Once the interaction is done, you’re going to want to disconnect the MouseButton1Event using :Disconnect().

1 Like

Sorry for being annoying, your method still working but after adding function for 2nd button aka skip button
it started to do same thing again

Script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local HRP = Character:FindFirstChild("HumanoidRootPart")

local Camera = workspace.CurrentCamera

local Interaction = script.Parent
local GUI = Interaction.Parent

local QuestGui = script.QuestGui
local MainBox = QuestGui["Main Box"]
local NevermindButton = QuestGui["Nevermind Button"]
local YesButton = QuestGui["Yes Button"]
local SkipButton = QuestGui["Skip Button"]

local NPCs = game.ReplicatedStorage.NPCs
local GetUnitName = game.ReplicatedStorage.GetUnitName

local Action = 0
local yesConnection
local nevermindConnection

Interaction.Event:Connect(function(Unit, Amount)
	Character:SetAttribute("AbleForInteractions", false)
	local NPC = GetUnitName:InvokeServer(Unit)
	
	MainBox.Parent = GUI
	NevermindButton.Parent = GUI
	YesButton.Parent = GUI
	
	MainBox.Text.Text = "Hi there, I have a job for you to complete. Are you interested?"
	Action = 1
	print("Changed to "..Action)
	
	yesConnection = YesButton.HitBox.MouseButton1Click:Connect(function()
		if Action == 1 then
			MainBox.Text.Text = "If you still haven't changed your mind, I will need you to defeat "..tostring(Amount).." "..tostring(NPC).."."
			Action = 2
			print("Changed to "..Action)
		elseif Action == 2 then
			MainBox.Text.Text = "Good Luck."
			Action = 0
			print("Changed to "..Action)
			
			NevermindButton.Parent = QuestGui
			YesButton.Parent = QuestGui
			
			task.spawn(function()
				task.wait(2)
				MainBox.Parent = QuestGui
				Character:SetAttribute("AbleForInteractions", true)
				yesConnection:Disconnect()
			end)
		end
	end)
	
	nevermindConnection = NevermindButton.HitBox.MouseButton1Click:Connect(function()
		nevermindConnection:Disconnect()
		
		MainBox.Text.Text = "Whatever."
		Action = 0
		print("Changed to "..Action)

		NevermindButton.Parent = QuestGui
		YesButton.Parent = QuestGui
		
		task.spawn(function()
			task.wait(1.5)
			MainBox.Parent = QuestGui
			Character:SetAttribute("AbleForInteractions", true)
		end)
	end)
end)

Here is the clip:

I know it might be caused of the fact that everytime i fire event it creates a new MouseButtonClick function but player doesn’t use it.
I tried putting Disconnect() to every place even possible but it still didnt give any result
I’m too stupid for all that sorry xd

You need to disconnect both connections at the end of the conversation. From what I’m seeing, you’re only disconnecting the one that the user is using.

1 Like

I was going crazy trying to figure out where exactly you said to disconnect the functions
TYSM it worked aswell

Final script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
local HRP = Character:FindFirstChild("HumanoidRootPart")

local Camera = workspace.CurrentCamera

local Interaction = script.Parent
local GUI = Interaction.Parent

local QuestGui = script.QuestGui
local MainBox = QuestGui["Main Box"]
local NevermindButton = QuestGui["Nevermind Button"]
local YesButton = QuestGui["Yes Button"]
local SkipButton = QuestGui["Skip Button"]

local NPCs = game.ReplicatedStorage.NPCs
local GetUnitName = game.ReplicatedStorage.GetUnitName

local Action = 0
local yesConnection
local nevermindConnection

Interaction.Event:Connect(function(Unit, Amount)
	Character:SetAttribute("AbleForInteractions", false)
	local NPC = GetUnitName:InvokeServer(Unit)
	
	MainBox.Parent = GUI
	NevermindButton.Parent = GUI
	YesButton.Parent = GUI
	
	MainBox.Text.Text = "Hi there, I have a job for you to complete. Are you interested?"
	Action = 1
	print("Changed to "..Action)
	
	yesConnection = YesButton.HitBox.MouseButton1Click:Connect(function()
		if Action == 1 then
			MainBox.Text.Text = "If you still haven't changed your mind, I will need you to defeat "..tostring(Amount).." "..tostring(NPC).."."
			Action = 2
			print("Changed to "..Action)
		elseif Action == 2 then
			MainBox.Text.Text = "Good Luck."
			Action = 0
			print("Changed to "..Action)
			
			NevermindButton.Parent = QuestGui
			YesButton.Parent = QuestGui
			
			task.spawn(function()
				task.wait(2)
				MainBox.Parent = QuestGui
				Character:SetAttribute("AbleForInteractions", true)
				
				yesConnection:Disconnect()
				nevermindConnection:Disconnect()
			end)
		end
	end)
	
	nevermindConnection = NevermindButton.HitBox.MouseButton1Click:Connect(function()
		MainBox.Text.Text = "Whatever."
		Action = 0
		print("Changed to "..Action)

		NevermindButton.Parent = QuestGui
		YesButton.Parent = QuestGui
		
		task.spawn(function()
			task.wait(2)
			MainBox.Parent = QuestGui
			Character:SetAttribute("AbleForInteractions", true)
			
			yesConnection:Disconnect()
			nevermindConnection:Disconnect()
		end)
	end)
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.