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

Why is my code not grabbing text off a webpage?

Asked by
P100D 590 Moderation Voter
8 years ago

So basically I have some code that will grab my game's version number from the game page. However, at line 5 it tells me that "ver" is a null value. What am I doing wrong?

function getVersion()
    local gameLink = "http://www.rproxy.pw/games/252491884/Defend"
    local html = game:GetService("HttpService"):GetAsync(gameLink, true)
    local pattern = [[<h1 class="game-name" title="Defend! [%d+.%d+.%d+] [Public InDev]">Defend! [0.2.5] [Public InDev]</h1>]]
    local ver = tostring(html:match(pattern):match("%d+.%d+.%d+"))
    return ver
end

while true do
    script.Parent.Text = getVersion()
    wait(60)
end

1 answer

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

"x-" is a special pattern. It does not mean an x followed by a hyphen. It means "0 or more "x" (but as few as possible)".

When you used a hyphen in your pattern, you meant the first, but Lua understands it as the second.

Similarly, [abc] is a pattern. It defines a character class, meaning one of a, b, or c.

When you used [, you meant it as a literal square bracket, but that's not how Lua understands it.


. is also a special character (meaning anything).

This one's probably not a problem, but it's also not what you mean. If there was a q there instead of a ., Lua would accept that.


You need to escape these special characters with %, such as %- or %[ or %.. This means that they are the literal character, e.g., %- matches a hyphen, not zero or more percent signs.

You can also use () to capture just the version, instead of using tostring and match

[[<h1 class="game%-name" title="Defend! %[(%d+%.%d+%.%d+)%] %[Public InDev%]">Defend! %[0%.2%.5%] %[Public InDev%]</h1>]]
Ad

Answer this question