0
votes

Currently, I'm working on a simple Lua Roblox script that is supposed to turn the parent part blue when "/blue" is entered in the chat by ANY player. When run, it returns the error "attempt to index global 'message' (a nil value)" in the output. Also, when I hover my cursor over "message" it says "unknown global 'message'". I am sure I'm doing something terribly wrong as I am new to the language. I have tried moving the script into Workspace and Chat (of course changing local part when I do) but those don't help. I'm confident it's a code issue specifically defining a global variable.

local part = script.Parent

local function scan()
    if message:sub(1,5) == "/blue" then
        part.BrickColor = BrickColor.Blue()
    end
end

scan()
1

1 Answers

0
votes

First, you didn't define "message" because "message" is supposed to be an argument of

player.Chatted()

So instead of just running scan(), make multiple functions, here is the revised code:

local part = script.Parent

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(message)
        message = string.lower(message)
        if message == "/blue" then
            part.BrickColor = BrickColor.new("Blue")
        end
    end)
end)

Let me know if you need me to elaborate, I understand that sometimes this stuff can be confusing.