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 6 years ago
Edited 6 years ago
001Namee = script.Parent.Namee
002 
003Nameo = script.Parent.Nameo
004 
005--Characters Here:
006 
007local 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"}
008 
009--Length Here:
010 
011local charLength = 1
012 
013local cache = {}
014 
015--/CONFIGURATION--
016 
017local Ui = script.Parent.Parent.ScrollingFrame
018 
019--You Will See Random Generated Word In Output Window
020 
021--To Start It Disable This Script And Enable
022 
023   
024 
025while true do wait()
026 
027local word = ""
028 
029for i = 1, charLength do
030 
031local randNum = math.random(1, #characters)
032 
033word = word .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)] .. characters[math.random(1, #characters)]
034 
035print(word)
036 
037Nameo.Text = word
038 
039wait()
040 
041word = ""
042 
043end
044 
045local Players = game:GetService("Players")
046 
047local cache = {}
048 
049function GetUserIdFromUsername(name)
050 
051-- First, check if the cache contains the name
052 
053if cache[name] then return cache[name] end
054 
055-- Second, check if the user is already connected to the server
056 
057local player = Players:FindFirstChild(name)
058 
059if player then
060 
061cache[name] = player.UserId
062 
063return player.UserId
064 
065end
066 
067-- If all else fails, send a request
068 
069local id
070 
071pcall(function ()
072 
073id = Players:GetUserIdFromNameAsync(name)
074 
075end)
076 
077cache[name] = id
078 
079return id
080 
081end
082 
083if (GetUserIdFromUsername(Nameo.Text)) == nil then
084 
085Namee.Text = ("Not Taken")
086 
087local addtolist = Instance.new("TextLabel")
088 
089addtolist.Parent = Ui
090 
091addtolist.BackgroundTransparency = 1
092 
093addtolist.Size = UDim2.new(5, 0, 5, 0)
094 
095addtolist.Text = Nameo.Text
096 
097script.Parent.Image = "rbxassetid://891981050"
098 
099else
100 
101Namee.Text = (GetUserIdFromUsername(Nameo.Text))
102 
103script.Parent.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=200&y=200&Format=Png&userid="..(GetUserIdFromUsername(Nameo.Text))
104 
105end
106 
107end

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()

01if Nameo.Text == string.sub(1,1) == "_" then
02 
03Nameo.Text = "null"
04 
05end
06 
07if Nameo.Text string.sub(4,4) == "_" then
08 
09Nameo.Text = "null"
10 
11end

It didn’t work…

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

1 answer

Log in to vote
2
Answered by 6 years ago
Edited 6 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:

1local s = "_Hello_";
2s = string.gsub(s, "_", "!");
3print(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:

1local s = "_Hello_";
2s = string.gsub(s, "_", "!", 1);
3print(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 — 6y
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 — 6y
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 — 6y
0
i have this local s = word; s = string.gsub(s, "_", "m"); print(s); JohnerDev 27 — 6y
View all comments (21 more)
0
Because its needed to generate the names, because some names have underscores JohnerDev 27 — 6y
0
That'd just replace all the underscores with the letter m. davidgingerich 603 — 6y
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 — 6y
0
I'm having trouble understanding exactly what it is that you're trying to do. davidgingerich 603 — 6y
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 — 6y
0
and roblox is wierd so u can put it in the middle between the beginning and the end of the name JohnerDev 27 — 6y
0
I'll edit my answer to show you how to do this. davidgingerich 603 — 6y
0
ok thanks JohnerDev 27 — 6y
0
What about the last one _oi_ JohnerDev 27 — 6y
0
There is 2 underscores and this would only get rid of one. JohnerDev 27 — 6y
0
Calling the function like string.gsub("_oi_", "_", "m", 1) would return "moi_" davidgingerich 603 — 6y
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 — 6y
0
But you don't know the exact characters before hand JohnerDev 27 — 6y
0
Not with that function, but I went ahead and made you one: https://pastebin.com/83HzyZdN davidgingerich 603 — 6y
0
Doing ReplaceOuterCharacters("_____", "_", "!") will return "!___!" davidgingerich 603 — 6y
0
thx for helping me JohnerDev 27 — 6y
0
how would i put the randomly generated word into it though just where the _____ is? JohnerDev 27 — 6y
0
oh maybe put word where the _____ is JohnerDev 27 — 6y
0
idk JohnerDev 27 — 6y
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 — 6y
0
ok JohnerDev 27 — 6y
Ad

Answer this question