NPC Chat Code not working

Hey Developers,

I’m trying to make a NPC talk a random order that has been generated by the server, however I can’t get it to work and I can’t find any console errors. I’ll show my code below so you can take a look!

Server Code

local Orders = {"Cookie","White Chocolate Cookie","Blue Cookie","random food"}
local RandomOrderEvent = game.ReplicatedStorage.RandomFoodEvent
--Create random food order
local randomorder = Orders[math.random(1, #Orders)] 

--Functions

local function loadnpcs()
RandomOrderEvent:FireAllClients(randomorder)
end

--MainCode
loadnpcs()

NPC Code

local RandomOrderEvent = game.ReplicatedStorage.RandomFoodEvent

RandomOrderEvent.OnClientEvent:Connect(function(randomorder)
game:GetService("Chat"):Chat(script.Parent.Head, randomorder, Enum.ChatColor.Green)
end)

I’m not really sure what I’m doing wrong as I’m quite new to this, any help would be appreciated. :heart:

Have you considered watching YouTube?
I found some tutorials, that may help

(Had an error linking videos but, you get the idea)

I hope I wasn’t that useless :sweat_smile:
7 :money_with_wings:

Try printing out randomorder, see if actually returns a string or nil.

I’m working on a cafe game with a mechanic your like NPC chatting, and I found this post. I’m happy to say you need to press “Play” or “Play here” not “Run”

This only works if a player plays the game not if a server is watching it

Hopes this helps!

Thanks :slight_smile:

You’re not calling your function. So it doesn’t run anything. You’re only defining it. If you want to call it, you would have to do loadnpcs() somewhere