I am writing a script so that when a specified player chats a message then the GUI window appears. Below is the script I have been using, however when I run the script I get no output. I have tried debugging and found nothing wrong.
local clearance = { "EmperorD" } local start = ":startapps" local stop = ":stopapps" local function checkClearance(name) for i = 1,#clearance do if (string.upper(name) == string.upper(clearance[i])) then return true end end return false end local function onMessaged(msg, recipient, speaker) local source = string.lower(speaker.Name) if msg == start or msg == stop then if (checkClearance(source)) then if msg == start then script.Parent.Visible = true end if msg == stop then script.Parent.Visible = false end end end end function onPlayerJoin(newPlayer) newPlayer.Chatted:connect(function(msg, recipient) onMessaged(msg, recipient, newPlayer) end) end game.Players.ChildAdded:connect(onPlayerJoin)
Notice that you're referring to script.Parent
-- an object inside the PlayerGui, but you're listening to Players.ChildAdded
.
Hence, if, while you are alive, someone else joins, and they chat, then it will listen to them.
But if they didn't join while you were spawned, there wasn't a ChildAdded
event to fire. So it never listens to the Chatted
event.
Simple solution: just call onPlayerJoin
on all present players:
for _, player in pairs(game.Players:GetPlayers()) do onPlayerJoin(player) end
At the same time, your original script was poorly formatted, and a little over long.
Good style makes code more beautiful! Eg., clearances
for the list of individual clearance
s:
local clearances = { "EmperorD" } local function checkClearance(name) for _, clearance in pairs(clearances) do if name:upper() == clearance:upper() then return true end end return false end
In addition, overall tabbing.
Also, the check if msg == start or msg == stop then
is redundant, so you can remove that altogether.
Similarly, taking string.lower
of speaker.Name
is redundant since checkClearance
explicitly compares ignoring case.