Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Custom chat script returns error. How can I fix this?

Asked by 6 years ago

I made a custom chat for my game. When I use team test to test it, it returns an error saying "attempt to index local 'info' (a nil value)". The script that it is erroring from is localscript "ChatScriptLocal" in the ScreenGui object that contains the chat gui. I looked through it, and I can't tell exactly what is wrong with it.

Here is ChatScriptLocal:

--Made by AppleBLOXIAN

--//Variabes
local main = script.Parent:WaitForChild("Main")
local chatframe = main:WaitForChild("Chat")
local chatbox = main:WaitForChild("ChatBox")
local sendbutton = main:WaitForChild("Send")
local rep = game:GetService("ReplicatedStorage")
local userinput = game:GetService("UserInputService")
local serverchat = rep:WaitForChild("ServerChat")
local chat = rep:WaitForChild("Chat")
local adminchat = rep:WaitForChild("AdminChat")
local settings = require(rep:WaitForChild("ChatSettings"))

--//Functions
function addchat(name,msg,isserver,isadmin,iscontinued)
    --Create message object
    local chatmsg = Instance.new("Frame",chatframe)
    chatmsg.Name = "ChatMsg"
    chatmsg.BackgroundColor3 = Color3.fromRGB(255,255,255)
    chatmsg.BackgroundTransparency = 1
    chatmsg.Size = UDim2.new(1,0,0,15)
    if not isserver or iscontinued then
        local namelabel = Instance.new("TextLabel",chatmsg)
        namelabel.Name = "Name"
        namelabel.BackgroundColor3 = Color3.fromRGB(255,255,255)
        namelabel.BackgroundTransparency = 1
        namelabel.Position = UDim2.new(0,0,0,0)
        namelabel.Font = Enum.Font.Arial
        namelabel.TextSize = 15
        if isadmin then
            if not name == "AppleBLOXIAN" then
                namelabel.Text = "[Admin]" .. name .. ":"
                namelabel.TextColor3 = Color3.fromRGB(255,0,0)
            else
                namelabel.Text = "[Owner]" .. name .. ":"
                namelabel.TextColor3 = Color3.fromRGB(0,0,255)
            end
        else
            namelabel.Text = name .. ":"
            namelabel.TextColor3 = Color3.fromRGB(0,255,0)
        end
        namelabel.Size = UDim2.new(0,namelabel.TextBounds.X,0,15)
        local msglabel = Instance.new("TextLabel",chatmsg)
        msglabel.Name = "Message"
        msglabel.BackgroundTransparency = 1
        msglabel.Position = UDim2.new(0,namelabel.TextBounds.X+2,0,0)
        msglabel.Size = UDim2.new(0,128,0,15)
        msglabel.Font = Enum.Font.Arial
        msglabel.Text = (function()
            local function isadmin(plr)
                for i,v in pairs(settings.Admins) do
                    if plr.name == v then
                        return true
                    else
                        return false
                    end
                end
            end
            msglabel.Text = msg
            if msglabel.TextBounds.X > chatmsg.AbsoluteSize.X then
                addchat("",msg:sub(msglabel.TextBounds.X+1),false,isadmin(game.Players.LocalPlayer),true)
                msglabel.Text = ""
                return msg:sub(1,chatmsg.AbsoluteSize.X)
            else
                return msg
            end
        end)()
        msglabel.TextColor3 = Color3.fromRGB(0,0,0)
        msglabel.TextSize = 15
    else
        if isserver then
            local msglabel = Instance.new("TextLabel",chatmsg)
            msglabel.Name = "Message"
            msglabel.BackgroundTransparency = 1
            msglabel.Position = UDim2.new(0,0,0,0)
            msglabel.Size = UDim2.new(0,128,0,15)
            msglabel.Font = Enum.Font.Arial
            msglabel.Text = (function()
                local function isadmin(plr)
                    for i,v in pairs(settings.Admins) do
                        if plr.Name == v then
                            return true
                        else
                            return false
                        end
                    end
                end
                msglabel.Text = msg
                if msglabel.TextBounds.X > chatmsg.AbsoluteSize.X then
                    addchat("",msg:sub(msglabel.TextBounds.X+1),true,isadmin(game.Players.LocalPlayer),true)
                    msglabel.Text = ""
                    return msg:sub(1,chatmsg.AbsoluteSize.X)
                else
                    return msg
                end
            end)()
            msglabel.TextColor3 = Color3.fromRGB(153,153,153)
            msglabel.TextSize = 15
        else
            local msglabel = Instance.new("TextLabel",chatmsg)
            msglabel.Name = "Message"
            msglabel.BackgroundTransparency = 1
            msglabel.Position = UDim2.new(0,0,0,0)
            msglabel.Size = UDim2.new(0,128,0,15)
            msglabel.Font = Enum.Font.Arial
            msglabel.Text = (function()
                local function isadmin(plr)
                    for i,v in pairs(settings.Admins) do
                        if plr.Name == v then
                            return true
                        else
                            return false
                        end
                    end
                end
                msglabel.Text = msg
                if msglabel.TextBounds.X > chatmsg.AbsoluteSize.X then
                    addchat("",msg:sub(msglabel.TextBounds.X+1),false,isadmin(game.Players.LocalPlayer),true)
                    msglabel.Text = ""
                    return msg:sub(1,chatmsg.AbsoluteSize.X)
                else
                    return msg
                end
            end)()
            msglabel.TextColor3 = Color3.fromRGB(0,0,0)
            msglabel.TextSize = 15
        end
    end
    --Push other messages
    chatmsg.Position = UDim2.new(0,0,0,chatframe.Size.Y.Offset)
    for i,v in pairs(chatframe:GetChildren()) do
        v:TweenPosition(v.Position - UDim2.new(0,0,0,15),"Out","Linear")
        if v.Position < 0 then
            v:Destroy()
        end
    end
end

function sendchat(msg)
    --Check if admin
    local function isadmin(plr)
        for i,v in pairs(settings.Admins) do
            if plr.Name == v then
                return true
            else
                return false
            end
        end
    end
    --Fire specific RemoteEvent for admin and non-admin accounts
    if isadmin(game.Players.LocalPlayer) then
        adminchat:FireServer(msg)
    else
        chat:FireServer(msg)
    end
end

function inputbegan(i,g)
    --End if game didnt process event
    if g then return end
    --Focus on chatbox if slash pressed, send message when enter pressed
    if i.KeyCode == Enum.KeyCode.Slash then
        if not chatbox:IsFocused() then
            chatbox:CaptureFocus()
        end
    elseif i.KeyCode == Enum.KeyCode.KeypadEnter then
        if chatbox:IsFocused() then
            if not chatbox.Text == "" then
                sendchat(chatbox.Text)
                chatbox.Text = ""
            end
        end
    end
end

function buttonclicked()
    --Spam prevention
    if not chatbox.Text == "" then
        sendchat(chatbox.Text)
        chatbox.Text = ""
    end
end

function onclientevent(info)
    --Check if admin
    local function isadmin(plr)
        for i,v in pairs(settings.Admins) do
            if plr.Name == v then
                return true
            else
                return false
            end
        end
    end
    --Check if server message
    if info.isserver then
        addchat("",info.message,true,false,false)
    else
        addchat(info.name,info.message,false,isadmin(info.sender),false)
    end
end

--//Connect events
chat.OnClientEvent:connect(onclientevent())
adminchat.OnClientEvent:connect(onclientevent())
serverchat.OnClientEvent:connect(onclientevent())
sendbutton.MouseButton1Click:connect(buttonclicked())
userinput.InputBegan:connect(inputbegan())

--//Disable core gui
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Chat,false)

This is a server sided script used to post chat messages:

--Made by AppleBLOXIAN

--//Variables
local rep = game:GetService("ReplicatedStorage")
local serverchat = rep:WaitForChild("ServerChat")
local chat = rep:WaitForChild("Chat")
local adminchat = rep:WaitForChild("AdminChat")
local settings = require(rep:WaitForChild("ChatSettings"))

--//Functions
function postmessage(info)
    if info.isserver == true then
        serverchat:FireAllClients(info)
    elseif info.isadmin == true then
        adminchat:FireAllClients(info)
    else
        chat:FireAllClients(info)
    end
end

function onserverevent(plr,msg)
    --Check if admin
    local function isadmin(plr)
        for i,v in pairs(settings.Admins) do
            if plr.Name == v then
                return true
            else
                return false
            end
        end
    end
    --Post message
    postmessage({
        sender = plr,
        name = plr.Name,
        message = msg,
        isserver = false,
        isadmin = isadmin(plr)
    })
end

function onplayerjoined(plr)
    postmessage({
        sender = "Server",
        name = "Server",
        message = plr .. " joined the game.",
        isserver = true,
        isadmin = false
    })
end

function onplayerleft(plr)
    postmessage({
        sender = "Server",
        name = "Server",
        message = plr .. " left the game.",
        isserver = true,
        isadmin = false
    })
end

--//Connect events
adminchat.OnServerEvent:connect(onserverevent)
serverchat.OnServerEvent:connect(onserverevent)
chat.OnServerEvent:connect(onserverevent)
game.Players.PlayerAdded:connect(onplayerjoined)
game.Players.PlayerRemoving:connect(onplayerleft)

This is a module containing settings for the chat:

local settings = {
    admins = {"AppleBLOXIAN","BlizzardBOY390","Cyrusthegreat2008","MythiicalPhoenix"},
    banned = {},
    msgonjoingame = true,
    msgonleftgame = true
}

return settings

1 answer

Log in to vote
0
Answered by 6 years ago

I went through and it might be because your function onclientevent has a parameter call info that when called, wasn't provided a parameter so it errors at the first attempt to use it because the parameter is nil. The only time info is mentioned on the client script is on this event:

function onclientevent(info) -- Maybe you didn't provide the info this function needs. Line 185
    --Check if admin
    local function isadmin(plr)
        for i,v in pairs(settings.Admins) do
            if plr.Name == v then
                return true
            else
                return false
            end
        end
    end
    --Check if server message
    if info.isserver then -- error probably occurs here, where info is first mentioned in the script.
        addchat("",info.message,true,false,false)
    else
        addchat(info.name,info.message,false,isadmin(info.sender),false)
    end
end

There's also only one place onclientevent is called, which is here:

chat.OnClientEvent:connect(onclientevent()) -- Line 205
adminchat.OnClientEvent:connect(onclientevent())
serverchat.OnClientEvent:connect(onclientevent())

One of those must've called it, but they didn't provide parameters so info is nil. The if function tries to detect if info.isserver is true, but since info is nil, it errors because it isn't true, and it isn't false. You either add the info for the parameter info, or you find a way around it.

Ad

Answer this question