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

Chat bot not working with string.sub, can anyone show me why?

Asked by 4 years ago

I have created a chat-bot, however whenever I run it, and try to find the age of the player it gives the players name properly, but it is always in lower case letters. I know exactly where the problem is, but how exactly do I slice strings and keep them to what the message said (ex. uppercase, lowercase letters kept.)

function Chat(msg)
    local message = msg
    local stuff = "All"
 game:GetService("ReplicatedStorage").DefaultChatSystemChatEvents.SayMessageRequest:FireServer(message, stuff)
end
Chat('This is Frometa1998\'s bot. Say cmds or commands to view the commands for this bot.')
for i,v in pairs(game.Players:GetChildren()) do 
    v.Chatted:Connect(function(msg)
        msg = msg:lower()
        if msg == 'cmds' or msg == 'commands' then
            Chat('The commands are spam: Put the message to spam after spam:,hi and bye.')
        elseif string.sub(msg,1,5) == 'spam:' then 
            spammsg = string.sub(msg,6,#msg)
            for i = 1,5,1 do 
                Chat(spammsg)
            end
        elseif msg == 'hi' then 
            Chat('Hello, '..v.Name..'!')
        elseif msg == 'bye' then 
            Chat('Have a nice day, '..v.Name..'!')
        elseif string.sub(msg,1,4) == 'age:' then -- This is where I am having my problem.
            person = game.Players:FindFirstChild(string.sub(msg,5,#msg))
            ageOfPerson = person.AccountAge
            Chat(person.Name..' account age today is '..ageOfPerson..'.')
        end
    end)
end
game.Players.PlayerAdded:Connect(function(plr)
    Chat(plr.Name..' has joined the game.')
    plr.Chatted:Connect(function(msg)
        msg = msg:lower()
        if msg == 'cmds' or msg == 'commands' then
            Chat('The commands are spam: Put the message to spam after spam:,hi and bye.')
        elseif string.sub(msg,1,5) == 'spam:' then 
            spammsg = string.sub(msg,6,#msg)
            for i = 1,5,1 do 
                Chat(spammsg)
            end
        elseif msg == 'hi' then 
            Chat('Hello, '..plr.Name..'!')
        elseif msg == 'bye' then 
            Chat('Have a nice day, '..plr.Name..'!')
        elseif string.sub(msg:lower(),1,4) == 'age:' then 
            person = game.Players[string.sub(msg,5,#msg)]
            ageOfPerson = person.AccountAge
            Chat(person.Name..' account age today is '..ageOfPerson..'.')
        end
    end)
end)
game.Players.PlayerRemoving:Connect(function(plr)
    Chat(plr.name..' has left the game.')
end)

Basically this is a chat bot, and so I just want string.sub to not lowercase the message sent. P.S this is run on the client (yes its bad practice)

Answer this question