My functions are acting weird

Hey gang! It’s been awhile since I’ve coded stuff and I came across a glitch in my code.
For some reason, the functions activate at complete random. Then the ArrayAdder Value just goes haywire and everything breaks. I genuinely dont know how to fix this.

Dialogue is supposed to activate when you touch these walls but sometimes activates at the wrong spots and goes haywire

Here’s the code, I genuinely don’t know what’s causing these issues. It doesn’t make sense to me

local Workspace =  game.Workspace

local Checker = 0

local Player = game:GetService("Players").LocalPlayer

local DialogueNumbers = 0

local GUI = script.Parent

local NextBTN = GUI.NextButtonFrame.NextButton

local TalkSFX = game.Workspace.SFX.TalkingSFX

local Text = GUI.DialogueBox.Dialogue

local ArrayAdder = 1



local StartingDialogue = {"The day has ended.", "After a long shift.", "What now.",
	"There's really no future to look forward to.", "I'll just go back to bed and remember.", "Just remember everything that made life worth living.",
	"However.", "Those are just memories.", "I'd kill to go back to a time that...", "I was happy."
}

local OutsideDialogue = {"Oh.", "It's that restaurant.", '"The Honest Mike"', "My parents used to take me here all the time.", "It was a great restaurant.",
	"I'd love to relive my moments there.", "However.", "I'm broke.", "Maybe if I wasn't this much of a loser I wouldn't be."
	
}

local HomeDialogue = {"I'm home.", "I'm finally back.", "After a long day.", "I still wonder why my friend installed that sign on top of my house.",
	"I'm never letting her do my architecture ever again.", "Her.", "HER.", "Whatever, it's in the past.", "It..", "Still matters."
}

local Activators = Workspace.Activators
-- Dialogue Numbers
local function DialogueNumber1()
	DialogueNumbers = 1
end

local function DialogeNumber2()
	DialogueNumbers = 2 
end


-- Dialogues

local function Start()
if DialogueNumbers == 0 then 
	if Checker == 0 then 
	Player.Character:FindFirstChild("Humanoid").JumpHeight = 0
	Player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
	NextBTN.Visible = false
	if DialogueNumbers == 0 and ArrayAdder < #StartingDialogue + 1  then 
		for count = 1, #StartingDialogue[ArrayAdder] do 
		TalkSFX.Playing = true
		Text.Text = string.sub(StartingDialogue[ArrayAdder],1, count)
		wait(0.07)
		end
	elseif ArrayAdder == #StartingDialogue + 1 then
		Player.Character:FindFirstChild("Humanoid").WalkSpeed = 11
		GUI.Enabled = false
		ArrayAdder = 0 
	end
	ArrayAdder = ArrayAdder + 1
	NextBTN.Visible = true
	TalkSFX.Playing = false
	end
	end
end

local function MidWay()
	if Player.Character:FindFirstChild("Humanoid") then 
if DialogueNumbers == 1 then 
	Activators:WaitForChild("MidWayActivator").CanTouch = false
	GUI.Enabled = true
	if ArrayAdder < #OutsideDialogue + 1 then 
		Player.Character:FindFirstChild("Humanoid").WalkSpeed = 0
		NextBTN.Visible = false
		for i = 1, #OutsideDialogue[ArrayAdder] do 
			Text.Text = string.sub(OutsideDialogue[ArrayAdder], 1, i)
			TalkSFX.Playing = true 
			wait(0.07)
		end
	end
	if ArrayAdder == #OutsideDialogue + 1 then
			ArrayAdder = 0 
			Player.Character:FindFirstChild("Humanoid").WalkSpeed = 11
			GUI.Enabled = false
		end
	ArrayAdder = ArrayAdder + 1
	NextBTN.Visible = true
	TalkSFX.Playing = false
	end
	end 
end

local function Home()
if Player.Character:FindFirstChild("Humanoid") then 
if DialogueNumbers == 2 then
	Activators:WaitForChild("HomeActivator").CanTouch = false
	Player.Character:FindFirstChild('Humanoid').WalkSpeed = 0
	NextBTN.Visible = false
	GUI.Enabled = true 
	if  ArrayAdder < #HomeDialogue + 1 then 
		for i = 1, #HomeDialogue[ArrayAdder] do 
			Text.Text = string.sub(HomeDialogue[ArrayAdder], 1, i)
			TalkSFX.Playing = true
			wait(0.07)
		end
	end
	if  ArrayAdder == #HomeDialogue + 1 then 
			Player.Character:FindFirstChild('Humanoid').WalkSpeed = 11
			ArrayAdder = 0 
			GUI.Enabled = false 
	end
	ArrayAdder = ArrayAdder + 1
	NextBTN.Visible = true 
	TalkSFX.Playing = false
	end
end
end



-- Activations
Start()
Activators:WaitForChild("MidWayActivator").Touched:Connect(MidWay)
Activators:WaitForChild("MidWayActivator").Touched:Connect(DialogueNumber1)
Activators:WaitForChild("HomeActivator").Touched:Connect(Home)
Activators:WaitForChild("HomeActivator").Touched:Connect(DialogeNumber2)


-- NextButtonStuff

NextBTN.MouseButton1Click:Connect(Start)
NextBTN.MouseButton1Click:Connect(MidWay)
NextBTN.MouseButton1Click:Connect(Home)


while true do 
	-- dont mind this.
	print("DialogueNumber = " .. DialogueNumbers)
	print("ArrayAdder = ".. ArrayAdder)
	wait(1)
end




```
2 Likes

Didn’t look through to see exactly what you’re doing but this jumped out to me. Having multiple functions connected to the same event seems potentially problematic. I’m fairly certain there is no guaranteed order for when signals receive the event.

1 Like

Well actually reddit figured it out. Turns out that I just needed something to check if the thing that activated it was a player with a humanoid in functions dialoguenumber 1 and dialogenumber2.

-- Dialogue Numbers
local function DialogueNumber1(Parameter)
	if Parameter.Parent:FindFirstChild("Humanoid") then
	DialogueNumbers = 1
	end
end

local function DialogeNumber2(Parameter)
	if Parameter.Parent:FindFirstChild("Humanoid") then
	DialogueNumbers = 2 
	end
end

But thanks for the tip! I’ll keep that in mind

You should mark your post as solution if you found the solution already

1 Like

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