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

How would I change the characters in a value, like if i have _c8_ change the _ to a letter?

Asked by 5 years ago
Edited 5 years ago
Namee = script.Parent.Namee

Nameo = script.Parent.Nameo

--Characters Here:

local characters = {"_", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}

--Length Here:

local charLength = 1

local cache = {}

--/CONFIGURATION--

local Ui = script.Parent.Parent.ScrollingFrame

--You Will See Random Generated Word In Output Window

--To Start It Disable This Script And Enable



while true do wait()

local word = ""

for i = 1, charLength do

local randNum = math.random(1, #characters)

word = word .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)]

print(word)

Nameo.Text = word

wait()

word = ""

end

local Players = game:GetService("Players")

local cache = {}

function GetUserIdFromUsername(name)

-- First, check if the cache contains the name

if cache[name] then return cache[name] end

-- Second, check if the user is already connected to the server

local player = Players:FindFirstChild(name)

if player then

cache[name] = player.UserId

return player.UserId

end

-- If all else fails, send a request

local id

pcall(function ()

id = Players:GetUserIdFromNameAsync(name)

end)

cache[name] = id

return id

end

if (GetUserIdFromUsername(Nameo.Text)) == nil then

Namee.Text = ("Not Taken")

local addtolist = Instance.new("TextLabel")

addtolist.Parent = Ui

addtolist.BackgroundTransparency = 1

addtolist.Size = UDim2.new(5, 0, 5, 0)

addtolist.Text = Nameo.Text

script.Parent.Image = "rbxassetid://891981050"

else

Namee.Text = (GetUserIdFromUsername(Nameo.Text))

script.Parent.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=200&y=200&Format=Png&userid="..(GetUserIdFromUsername(Nameo.Text))

end

end

I have tried using strings but it didn't work, and I don't even know if there is a way. Nameo is the value because it comes back like i said in the title, sometimes.

I added this below Nameo.Text = word and above wait()

if Nameo.Text == string.sub(1,1) == "_" then

Nameo.Text = "null"

end

if Nameo.Text string.sub(4,4) == "_" then

Nameo.Text = "null"

end

It didn't work...

0
I'd like only the underscores to be removed JohnerDev 27 — 5y
0
string.sub Mr_Unlucky 1085 — 5y
0
like if string.sub(1,1) == "_" then or something Mr_Unlucky 1085 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Luckily for you, Lua provides a method for this. You can use string.gsub, the first argument is the string, the second is the thing you want to replace in the string, and the third is is what you want to replace it with.

Example:

local s = "_Hello_";
s = string.gsub(s, "_", "!");
print(s);

The result is --> !Hello!

EDIT: An optional fourth argument also allows you to specify how many times the function is allowed to replace something.

Example:

local s = "_Hello_";
s = string.gsub(s, "_", "!", 1);
print(s);

The result is --> !Hello_

https://www.lua.org/pil/20.1.html

0
THANK YOU SO MUCH, I NEVER KNEW HOW TO USE GSUB greatneil80 2647 — 5y
0
what if i only want it to be removed in certain areas like _c_8 instead of the thing in the title? i want one underscore to be removed instead of both, like it would be !c_8 according to yours with what i want JohnerDev 27 — 5y
0
Why not just use different symbols? So instead _ being the symbol that gets replacing, you could use * as the symbol that gets replaced, so *c_8 would turn into !c_8 davidgingerich 603 — 5y
0
i have this local s = word; s = string.gsub(s, "_", "m"); print(s); JohnerDev 27 — 5y
View all comments (21 more)
0
Because its needed to generate the names, because some names have underscores JohnerDev 27 — 5y
0
That'd just replace all the underscores with the letter m. davidgingerich 603 — 5y
0
yeah but i only want it to replace the two on the outside _jd_ for example those 2 i want to replace with m because it doesnt matter since that account probably already exists JohnerDev 27 — 5y
0
I'm having trouble understanding exactly what it is that you're trying to do. davidgingerich 603 — 5y
0
Im trying to make a 4 letter name generator that includes underscores but roblox doesn't allow underscores at the beginning or the end of the name JohnerDev 27 — 5y
0
and roblox is wierd so u can put it in the middle between the beginning and the end of the name JohnerDev 27 — 5y
0
I'll edit my answer to show you how to do this. davidgingerich 603 — 5y
0
ok thanks JohnerDev 27 — 5y
0
What about the last one _oi_ JohnerDev 27 — 5y
0
There is 2 underscores and this would only get rid of one. JohnerDev 27 — 5y
0
Calling the function like string.gsub("_oi_", "_", "m", 1) would return "moi_" davidgingerich 603 — 5y
0
So basically I want the underscores to only get replaced at certain spots, because lets say i had _9__ as the random thing that would go to m9mm whilst i would like it to be m9_m not that, or lets say t_9_ that would be tm9m or tm9_ but i would like t_9m in that case, so is there even a way to do this? JohnerDev 27 — 5y
0
But you don't know the exact characters before hand JohnerDev 27 — 5y
0
Not with that function, but I went ahead and made you one: https://pastebin.com/83HzyZdN davidgingerich 603 — 5y
0
Doing ReplaceOuterCharacters("_____", "_", "!") will return "!___!" davidgingerich 603 — 5y
0
thx for helping me JohnerDev 27 — 5y
0
how would i put the randomly generated word into it though just where the _____ is? JohnerDev 27 — 5y
0
oh maybe put word where the _____ is JohnerDev 27 — 5y
0
idk JohnerDev 27 — 5y
0
If you have Discord, you can PM me, I'm the the SH Discord server, and my name is the same one I have on here. In the meantime, would you be so kind to accept this as the correct answer? I also answered a question you had previous to this one which you never got back to. davidgingerich 603 — 5y
0
ok JohnerDev 27 — 5y
Ad

Answer this question