Face changing system for dialogue

  1. What do you want to achieve?
    Im trying to make a system that, when the proximity prompt is activated, and the dialogue starts, it constantly changes the NPC’s face to one of 3 random faces to simulate it talking.

  2. What is the issue? Include screenshots / videos if possible!
    So previously I was using an NPC dialogue system created by a Youtuber called “Bubblx”. After trying to make my previously stated system, the dialogue wouldn’t pop up, and the face never changed. Also, I couldn’t find any errors in the console.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find any solutions/tutorials for a system like what im trying to create.


Unmodified script
local NPC = script.Parent.Parent.Parent
local TalkEvent = game:GetService("ReplicatedStorage").Npc
local Prox = script.Parent

script.Parent.Triggered:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 0
	plr.Character.Humanoid.JumpHeight = 0
	local NpcName = NPC.Name
	local M1 = script.Parent.Message1.Value
	local M2 = script.Parent.Message2.Value
	local M3 = script.Parent.Message3.Value
	
	TalkEvent:FireClient(plr,NpcName, M1, M2, M3,Prox)
end)
TalkEvent.OnServerEvent:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 16
	plr.Character.Humanoid.JumpHeight = 7.2
	plr.Character.Humanoid.JumpPower = 50
end)

My script
local NPC = script.Parent.Parent.Parent
local TalkEvent = game:GetService("ReplicatedStorage").Npc
local Prox = script.Parent

function mouth()
	local faceid = math.random(1,3)
	local npcface = script.Parent.Parent.Parent.Head.face.Texture
	if faceid == 1 then
		npcface = 266304601
		wait(0.5)
	elseif faceid == 2 then
		npcface = 266304668
		wait(0.5)
	elseif faceid == 3 then
		npcface = 266304708
		wait(0.5)
	end
end

script.Parent.Triggered:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 0
	plr.Character.Humanoid.JumpHeight = 0
	local talking = true
	local NpcName = NPC.Name
	local M1 = script.Parent.Message1.Value
	local M2 = script.Parent.Message2.Value
	local M3 = script.Parent.Message3.Value
		
	while talking == true do
		mouth()
		wait(0.5)
	end
	
	TalkEvent:FireClient(plr,NpcName, M1, M2, M3,Prox)
	
	talking = false
end)
TalkEvent.OnServerEvent:Connect(function(plr)
	plr.Character.Humanoid.WalkSpeed = 16
	plr.Character.Humanoid.JumpHeight = 7.2
	plr.Character.Humanoid.JumpPower = 50

end)

PS. Im still a bit new to scripting, and this is my first ever post on the devforum.

1 Like

Okay, so I’m not too sure if this is related to your problem, but whenever you start a loop, nothing ahead of the code will run (unless it’s an event) until the loop ends.

Maybe try putting the TalkEvent & talking line’s before the loop? Sorry if this is intended, I kind of skimmed through this post.

I actually didn’t know thats how loops work.

Yup! I found something else too.

function mouth()
	local faceid = math.random(1,3)
	local npcface = script.Parent.Parent.Parent.Head.face.Texture
	if faceid == 1 then
		npcface = 266304601
		wait(0.5)
	elseif faceid == 2 then
		npcface = 266304668
		wait(0.5)
	elseif faceid == 3 then
		npcface = 266304708
		wait(0.5)
	end
end

If you try setting a texture to a number, you’ll get this:

image

To make the face properly apply, you’ll want to do this:

script.Parent.Head.face.Texture = "rbxassetid://266304601"

image

Then the face will apply :smiley:

Hope this helps you!

Thanks for that! Sadly, I still can’t figure out how to make the system work.