Need Help With Dialogue System

Hello, I’ve recently been working on a game that includes a similar dialogue system to camping. But I don’t know how to make the dialogue speaker a random player chosen from the game, help with this would be appreciated. Example of what I mean:

Script:

Players = game:GetService("Players")
Box = 1
local DialogGui =  script.Parent.Parent.PlayerGui:WaitForChild("NPC_Dialog_GU")
print("Found GUI")
local TextBox = DialogGui:FindFirstChild("NPC_Dialog_Tex")
local ImageFrame = DialogGui:FindFirstChild("Player_Imag")
local NameLabel = DialogGui:FindFirstChild("Player_Nam")
print("Found text box")
wait(5)
local LocalPlayer = Players.LocalPlayer
local PName = LocalPlayer.Name
local Text1 = "ok!"
local Text2 = "ok"

if LocalPlayer.Character then
	print("character loaded")
	local Character = LocalPlayer.Character
	wait(17.5)
	TextBox.Visible = true
	ImageFrame.Visible = true
	for i = 1, #Text1 do
		TextBox.Text = string.sub(Text1, 1, i)
		wait(0.05)
	end
	wait(4.5)
	ImageFrame.Image = Players:GetUserThumbnailAsync(LocalPlayer.UserId, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size352x352)
	NameLabel.Text = PName
	NameLabel.Visible = true
	wait(0.5)
	for i = 1, #Text2 do
		TextBox.Text = string.sub(Text2, 1, i)
		wait(0.05)
	end
	wait(5)
	TextBox.Visible = false
	DialogGui.Player_Imag.Visible = false
	NameLabel.Visible = false
end


To get a random player that is currently in the game, you can do this:

local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
local RandomPlayer = PlayerList[math.random(1,#PlayerList)]
local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
local RandomPlayer = PlayerList[math.random(1,#PlayerList)]
Box = 1
local DialogGui =  script.Parent.Parent.PlayerGui:WaitForChild("NPC_Dialog_GU")
print("Found GUI")
local TextBox = DialogGui:FindFirstChild("NPC_Dialog_Tex")
local ImageFrame = DialogGui:FindFirstChild("Player_Imag")
local NameLabel = DialogGui:FindFirstChild("Player_Nam")
print("Found text box")
wait(5)
local LocalPlayer = Players.LocalPlayer
local PName = LocalPlayer.Name
local Text1 = "ok!"
local Text2 = "ok"

if LocalPlayer.Character then
	print("character loaded")
	local Character = LocalPlayer.Character
	wait(17.5)
	TextBox.Visible = true
	ImageFrame.Visible = true
	for i = 1, #Text1 do
		TextBox.Text = string.sub(Text1, 1, i)
		wait(0.05)
	end
	wait(4.5)
	ImageFrame.Image = Players:GetUserThumbnailAsync(LocalPlayer.UserId, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size352x352)
	NameLabel.Text = PName
	NameLabel.Visible = true
	wait(0.5)
	for i = 1, #Text2 do
		TextBox.Text = string.sub(Text2, 1, i)
		wait(0.05)
	end
	wait(5)
	TextBox.Visible = false
	DialogGui.Player_Imag.Visible = false
	NameLabel.Visible = false
end

something like this?

Since this is a LocalScript, all players in game will show different usernames. Try this:

local Players = game:GetService("Players")
Box = 1
local DialogGui =  script.Parent.Parent.PlayerGui:WaitForChild("NPC_Dialog_GU")
print("Found GUI")
local TextBox = DialogGui:FindFirstChild("NPC_Dialog_Tex")
local ImageFrame = DialogGui:FindFirstChild("Player_Imag")
local NameLabel = DialogGui:FindFirstChild("Player_Nam")
print("Found text box")
wait(5)
local LocalPlayer = Players.LocalPlayer
local PName = LocalPlayer.Name
local Text1 = "ok!"
local Text2 = "ok"

if LocalPlayer.Character then
	print("character loaded")
	local Character = LocalPlayer.Character
	wait(17.5)
	TextBox.Visible = true
	ImageFrame.Visible = true
	for i = 1, #Text1 do
		TextBox.Text = string.sub(Text1, 1, i)
		wait(0.05)
	end
	wait(4.5)
        local PlayerList = Players:GetPlayers()
        local RandomPlayer = PlayerList[math.random(1,#PlayerList)]
	ImageFrame.Image = Players:GetUserThumbnailAsync(RandomPlayer.UserId, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size352x352)
	NameLabel.Text = RandomPlayer.Name; 
	NameLabel.Visible = true
	wait(0.5)
	for i = 1, #Text2 do
		TextBox.Text = string.sub(Text2, 1, i)
		wait(0.05)
	end
	wait(5)
	TextBox.Visible = false
	DialogGui.Player_Imag.Visible = false
	NameLabel.Visible = false
end
1 Like

Thanks for the help, the dialogue speaker is now randomly chosen

Those indentations before each “local” aren’t necessary.

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local PlayerName = Player.Name
local Character = Player.Character or Player.CharacterAdded:Wait()
local PlayerGui = Player:WaitForChild("PlayerGui")
local DialogGui =  PlayerGui:FindFirstChild("NPC_Dialog_GU")
print("Found GUI")
local TextBox = DialogGui:FindFirstChild("NPC_Dialog_Tex")
local ImageFrame = DialogGui:FindFirstChild("Player_Imag")
local NameLabel = DialogGui:FindFirstChild("Player_Nam")
print("Found text box")
local Text1 = "ok!"
local Text2 = "ok"
local Box = 1

task.wait(17.5)
TextBox.Visible = true
ImageFrame.Visible = true

for i = 1, #Text1 do
	TextBox.Text = string.sub(Text1, 1, i)
	task.wait()
end

task.wait(4.5)
local PlayerList = Players:GetPlayers()
local RandomPlayer = PlayerList[math.random(1,#PlayerList)]
ImageFrame.Image = Players:GetUserThumbnailAsync(RandomPlayer.UserId, Enum.ThumbnailType.AvatarBust, Enum.ThumbnailSize.Size352x352)
NameLabel.Text = RandomPlayer.Name
NameLabel.Visible = true
task.wait(0.5)

for i = 1, #Text2 do
	TextBox.Text = string.sub(Text2, 1, i)
	task.wait()
end

task.wait(5)
TextBox.Visible = false
ImageFrame.Visible = false
NameLabel.Visible = false

Some minor optimisations, wait() replaced with task.wait(), you can listen for CharacterAdded to determine that the local player’s character has loaded, PlayerGui is referenced from the player instance itself through which the descending UI instances are referenced and some other minor changes.