How to make a script keep checking if a condition is true and then make the script wait temporarily before continuing again

Hey there! This is my first post, so apologies for any mistakes I might make.

I have been scripting a game where you interact with a simple NPC, you can do this by just talking to it, answering its questions or by interacting with the environment around it. (For example flicking a light switch multiple times to annoy it)

Currently a main script decides what the NPC will say, and what I want is for the script to wait with continuing when you annoy it so it can get mad at you and say something like “Can you stop doing that?” before resuming its conversation.

This is sort of possible with

repeat wait() until interuption == false

Which will basically wait until the interruption function is done typewriting its line and sets interruption to false so the main script can continue.

However I would have to add this line of code before every single typewrite function for the NPC which seems extremely inefficient. I will put my main script below so you can take a look at it (I know my coding is bad)

-- Main Game Script that controls the dialogue and replies --

-- ROBLOX Services --
local RS = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

-- Instances --
local NPC = game.Workspace.NPC
local NPChum = NPC:WaitForChild("Humanoid")
local IdleAnim =  NPChum:LoadAnimation(script.Idle)
local WaveAnim = NPChum:LoadAnimation(script.Wave)
local TypewriterModule = require(RS:WaitForChild("TypewriterModule", 10))
local StartGame = RS:WaitForChild("StartGameEvent")

-- Variables --
local keyWords = {}
local response = nil
local ColourTable = {"red", "orange", "green", "yellow", "purple"}
local DoTable = {"not much"}

-- Functions --

-- This function capitalizes the first letter of a string. --
function firstToUpper(str)
	return (str:gsub("^%l", string.upper))
end


-- Reply controller --
game.Players.PlayerAdded:Connect(function(player)
	-- Get their Relevant info --
	local UserID = player.UserId
	local playerName = player.Name
	local playerDisplayName = player.DisplayName
	
	-- Wait for their character and immobilize it. --
	local Character = player.Character or player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	Humanoid.WalkSpeed = 0
	Humanoid.JumpHeight = 0
	
	-- Wait for a keyword --
	player.Chatted:Connect(function(message)
		local loweredstring = string.lower(message)
		if keyWords then
			for _, i in pairs(keyWords) do
				if string.match(loweredstring,i) then
					print("Found a keyword!")
					print("Player replied with: " ..loweredstring)
					response = loweredstring
					keyWords = nil
				end
			end
		end
	end)
	
	
	
	-- Main Code --
	local PlayerGui = player:WaitForChild("PlayerGui")
	local guiObject = PlayerGui:WaitForChild("DialogueGUI").Frame.LocalizedText
	IdleAnim:Play()
	StartGame.OnServerEvent:Wait()
	WaveAnim:Play()
	wait(1)
	TypewriterModule.typeWrite(guiObject,[[Good afternoon,]] .. playerDisplayName .. [[!]], 0.03)
	wait(4)
	TypewriterModule.typeWrite(guiObject,[[I hope you've been doing well today!<br /> <font size="25">[Reply to continue]</font>]], 0.03)
	player.Chatted:Wait()
	TypewriterModule.typeWrite(guiObject,[[So, what's your favorite color?]], 0.03)
	wait(2)
	keyWords = ColourTable
	repeat wait() until keyWords == nil
	if response == "purple" then
		TypewriterModule.typeWrite(guiObject,[[Purple? That's my favourite colour too!]], 0.03)
	else
		TypewriterModule.typeWrite(guiObject,firstToUpper(response)..[[? That's a nice colour!]], 0.03)
	end
	
	wait(4)
	TypewriterModule.typeWrite(guiObject,"So, what have you been up to today? ", 0.03)
    player.Chatted:Wait()
	TypewriterModule.typeWrite(guiObject,"I see, well I have been working on some fun projects! ", 0.03)
end)

You can see I already use the repeat wait() until for waiting for a response that matches the table of colours.

Any help is greatly appreciated!

I came up with this pretty fast, Idk if it will help but here it is!

while true  do
	if CONDITON == true then
		wait(5)
		--continue
	end
	wait()
end

In that case, why not just place the repeat wait loop within the typeWrite function. That way you would only have the line written once but it would still run with each function call.

1 Like

Hmm, that could work yeah!

Since the typeWrite function is in a different (module) script, would a bool value a good way for both scripts to see the value of the interruption condition?

You could also have a non local variable within the module function which you can then access from other scripts that have “required” the module. I personally find this easier, but a bool value would work fine.

Module Script

local moduleName {}

moduleName.VariableName = true

return moduleName

Local Script

local module = require(...) --Add Path to module
print(module.VariableName) --Should print true
1 Like

Oh! I’m still quite new to module scripts haha.
That would work perfectly, thanks so much!

I’ll mark your other reply as the solution as that answered my original question :slight_smile: