So I am trying to make NPCs say different things depending on the time of day. When I tried running this, it did not work. I got the idea from a time of day script and a chat script I found online, and I felt like doing a script like this would give more life to the NPCs.
wait (2) local ChatService = game:GetService("Chat") -- You can also add wait() function before this line if you want. local part = script.Parent.Head DayLines = {"Good Afternoon.","Where do you want to eat?","I heard there is a ghost haunting the school, creepy right?"} NightLines = {"We should be getting home.","I am sleepy...","zzzz"} MorningLines = {"Good Morning","What did you have for breakfast?","See you later!"} EveningLines = {"Night is soon, we should be going home.","It's getting dark...","See you tomorrow!"} while wait(2) do if game.Lighting.ClockTime > 6 and game.Lighting.ClockTime < 10 then local Chat = ChatService:Chat(part, [math.random(1,#MorningLines)], "White") wait(20) elseif game.Lighting.ClockTime > 10 and game.Lighting.ClockTime < 14 then local Chat = ChatService:Chat(part, [math.random(1,#DayLines)], "White") wait(20) elseif game.Lighting.ClockTime > 20 or game.Lighting.ClockTime < 6 then local Chat = ChatService:Chat(part, [math.random(1,#NightLines)], "White") wait(20) elseif game.Lighting.ClockTime > 14 and game.Lighting.ClockTime < 20 then local Chat = ChatService:Chat(part, [math.random(1,#EveningLines)], "White") wait(20) end end
Any ideas on how to make it work?
The problem is you aren't actually getting any info from the tables, instead you are just choosing a random number between 1 and #Lines. It should be for example:
local Chat = ChatService:Chat(part, MorningLines[math.random(1,#MorningLines)], "White")
so,
wait (2) local ChatService = game:GetService("Chat") -- You can also add wait() function before this line if you want. local part = script.Parent.Head DayLines = {"Good Afternoon.","Where do you want to eat?","I heard there is a ghost haunting the school, creepy right?"} NightLines = {"We should be getting home.","I am sleepy...","zzzz"} MorningLines = {"Good Morning","What did you have for breakfast?","See you later!"} EveningLines = {"Night is soon, we should be going home.","It's getting dark...","See you tomorrow!"} while wait(2) do if game.Lighting.ClockTime > 6 and game.Lighting.ClockTime < 10 then local Chat = ChatService:Chat(part, MorningLines[math.random(1,#MorningLines)], "White") wait(20) elseif game.Lighting.ClockTime > 10 and game.Lighting.ClockTime < 14 then local Chat = ChatService:Chat(part, DayLines[math.random(1,#DayLines)], "White") wait(20) elseif game.Lighting.ClockTime > 20 or game.Lighting.ClockTime < 6 then local Chat = ChatService:Chat(part, NightLines[math.random(1,#NightLines)], "White") wait(20) elseif game.Lighting.ClockTime > 14 and game.Lighting.ClockTime < 20 then local Chat = ChatService:Chat(part, EveningLines[math.random(1,#EveningLines)], "White") wait(20) end end
Should do.