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

String manipulation (string.match)?

Asked by
Gybron 19
9 years ago

This script 'ere finds the B0 from the IntValue called "pimppi". It increases the number after B by 2. But the issue is, how can I not affect the other values of pimppi? Line 16 nulls all the other values when B0 is changed. I could do the same for the other values in pimppi as I did to B0 here, but later on, that would be messy as hell. How do I do it with string.match? This is a really messy script anyway, I'm just trying to learn string manipulation!

Thank ye!

local clickDetector = script.Parent.ClickDetector

local number = "%d+"
local letter = "%w"
local stats = script.Parent.pimppi
local s = stats.Value

local function onMouseClick(player)

local Kappa = ( string.match(s, "B%w+") )
local Heathen = (string.match(Kappa, number) )
local Stender = (string.match(Kappa, letter) )
local bob = ("".. Stender .."".. Heathen + 2 .."")
Kappa = bob

s = ("A0 ".. Kappa .." C0 D0 E0")

print(Heathen)
print(Stender)
print("Kappa: " ..Kappa)
print(bob)
print(s)

end
clickDetector.MouseClick:connect(onMouseClick)
0
I would suppose you would use gsub, example s:gsub('B%d+', 'B' .. Kappa) The example would find B then any number following it in the string, then replace it with B, then the 'Kappa' you wanted. Though string manipulation isn't one of my strong suits, it might be beneficial to your problem. M39a9am3R 3210 — 9y

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
9 years ago

If you want to replace part of a string then use string.gsub.

With your string "A0 B0 C0 D0 E0", if you want to only match "B0" and increase the number by 1, then replace it and add one through the function argument.

local s = stats.Value:gsub("B(%d+)",function(n)return"B"..(tonumber(n)+1)end)

This captures B(%d+) which is B following any number with one or more digits, then captures that number and passes it to the function which adds one to it and returns the new string replacement.

Ad

Answer this question