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

How do I make it print any number I say between 1 & 100?

Asked by 7 years ago
Edited 7 years ago

I didn't know how to explain this one in the title. Anyway so I was messing around with return and I got an idea that involved math so I tried creating a script that printed out a sum of a math equation that the LocalPlayer says (Example: '1 + 1' -> '2') but I don't know how to make it detect the numbers of 1 to 100. Any help?

Edit: I was trying to use math.random but it doesn't seem to work because it picks 2 random numbers and not any value of 1 & 100 for what i'm trying to do.

function add(num1,num2)
    return num1 + num2
end

n1 = math.random(1,100)
n2 = math.random(1,100)

game.Players.LocalPlayer.Chatted:connect(function(msg)
    if msg:lower() == n1..' + '..n2 then
        local a = add(n1,n2)

        print(msg.." = "..a)
    end
end)
0
you want to create script which will find numbers and add them? Etheroit 178 — 7y
0
Yes, I want it to print the sum of the equation when the LocalPlayer chats '(Number) + (Number)' Odiosus_Ancilla 34 — 7y
0
Huh I did script. Ill post it in sec Etheroit 178 — 7y
0
Mk thank you. Odiosus_Ancilla 34 — 7y
View all comments (2 more)
0
Np Etheroit 178 — 7y
0
Also if You will want to add more functions like "/" or "*" then just look in commants of my answer ;) Etheroit 178 — 7y

2 answers

Log in to vote
0
Answered by
Etheroit 178
7 years ago
Edited 7 years ago

I made example script which would help You. Solution was using match and pattern. Thats how I would do this.

function Sum(n1, n2)
    return n1 + n2
end
function Subtract(n1, n2)
    return n1 - n2
end

game.Players.LocalPlayer.Chatted:connect(function(msg)
    -- GETTING ARGS
    local args = nil
    if string.match(msg, "%d+%s*%p%s*%d+") ~= nil then
        args = {string.match(msg, "(%d+)%s*(%p)%s*(%d+)")}
    end
    if args ~= nil then
        -- CHECKING ARGS
        if args[2] == "+" then
            print(Sum(tonumber(args[1]), tonumber(args[3])))
        elseif args[2] == "-" then
            print(Subtract(tonumber(args[1]), tonumber(args[3])))
        end
    end
end)

You can write for example "Whats 1 + 5" or just "1 + 5" and on console you will see "6" There is subtract function too "6 - 5" - "1" NOTE: Place spaces between numbers and characters EDIT: Edited code so it will work witheout making spaces now: "5+1" = "6"

Its easy to edit, just add things in the section wheres "CHECKING ARGS" comment. Example of args ("1 + 6") args[1] represents "1" args[2] represents "+" args[3] represents "6" You can also make new functions like "/", "*"

EDIT2: Its now not as huge as it was :)

0
Editted: You will need to place spaces between nums Etheroit 178 — 7y
0
This code is easy to edit also. Just clone last likes (There is "CHECKING ARGS") comment, so args[2] represents "+" or "-". You know this centered char in "1 + 5" args[1] represents "1" and args[3] represents "5" in this example. Etheroit 178 — 7y
1
Why use a while loop when you can just use string pattern captures Pyrondon 2089 — 7y
0
Uhh Ik, I took it from my own admin commands. Ima gonna edit it. Etheroit 178 — 7y
View all comments (3 more)
0
I don't understand match and pattern at all, could you explain it a bit so I get the basics of it? Odiosus_Ancilla 34 — 7y
0
Uh also editted script a little bit, its now smaller.This could help you with patterns: http://www.lua.org/pil/20.2.html and this with match function http://www.lua.org/pil/20.1.html. match function just looks in string for pattern, pattern uses special chars like (%d) which means like any digit, (%s) spaces, (%a) letter Also there are for example (%a+) which means to find more than one letter. Th Etheroit 178 — 7y
Ad
Log in to vote
1
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago
Edited 7 years ago

You can do what you're trying to through the use of string patterns and some useful string functions:

game.Players.LocalPlayer.Chatted:connect(function(msg)
    local n1, n2 = msg:match('(%d+)%s*%+%s*(%d+)');
    if n1 and n2 then
        n1 = tonumber(n1);
        n2 = tonumber(n2);
        if n1 > 0 and n1 < 101 and n2 > 0 and n2 < 101 then
            print(msg.." = "..n1+n2);
        end;
    end;
end)

Without understanding of what string patterns are and what they do, what's inside of the match function can seem like another language. I urge you to check out the above wiki page on string patterns.

Hope this helped.

EDIT: Edited to account for potential whitespace.

0
On the first code, I get an error saying 'attempt to compare number with nil'. On the second code it just does nothing. Odiosus_Ancilla 34 — 7y
0
Odd, edited & should work fine now. Pyrondon 2089 — 7y

Answer this question