Hello! Would someone be so kind to explain to me how you use and in what cases to use string gsub and split?
BASIC example on gsub
local text = "Hella Warld"
text = string.gsub(text--[[the string]],"a"--[[character to replace]],"o"--[[character to replace with]])
BASIC example on split
local text = "Hello World! Welcome to the universe!"
print(string.split(text--[[the string]]," "--[[splitting string]]))
2 Likes
Split
Lets say you want to have a command on chat, and you want to have this format :
command
target
Using Player.Chatted
, you’ll use split
to get those separate words, e.g:
game.Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
local split = Message:Split(" ") --- between the "" put the symbol you want to segregate the words with.
if split[1] == "!kill" then
if game.Players:FindFirstChild(split[2]) then
game.Workspace:FindFirstChild(split[2]).Humanoid.Health = 0
end
end
end)
end)
Gsub
Let’s say you have a message that you’d like to replace each time.[ A minigame intermission message can be an example].
local IntermissionText = "Round ends in 10 seconds"
local newText = IntermissionText:gsub("%d","#")
print(newText) -- will print 'Round ends in ## seconds'
Conclusion:
Split
Returns a table of strings. Very useful with admin commands. In the ("") you’ll need to provide any symbol that’ll segregate the strings.
Gsub
Replaces any provided key[string/number,etc] by the provided replacement.
1 Like
Thank you guys very much it was very helpful!