Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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...

3 comments

0

I'd like only the underscores to be removed

0

string.sub

0

like if string.sub(1,1) == "_" then or something

1 answer

✓ Accepted answer

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

25 comments

0

THANK YOU SO MUCH, I NEVER KNEW HOW TO USE GSUB

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

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

0

i have this local s = word; s = string.gsub(s, "_", "m"); print(s);

0

Because its needed to generate the names, because some names have underscores

0

That'd just replace all the underscores with the letter m.

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

0

I'm having trouble understanding exactly what it is that you're trying to do.

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

0

and roblox is wierd so u can put it in the middle between the beginning and the end of the name

0

I'll edit my answer to show you how to do this.

0

ok thanks

0

What about the last one oi

0

There is 2 underscores and this would only get rid of one.

0

Calling the function like string.gsub("oi", "", "m", 1) would return "moi"

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?

0

But you don't know the exact characters before hand

0

Not with that function, but I went ahead and made you one: https://pastebin.com/83HzyZdN

0

Doing ReplaceOuterCharacters("__", "_", "!") will return "!!"

0

thx for helping me

0

how would i put the randomly generated word into it though just where the _____ is?

0

oh maybe put word where the _____ is

0

idk

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.

0

ok

This discussion is preserved as it was. New answers, comments, and votes are closed.