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!