What the text should say:
“Change announcement Colours…” What the text says:
“Ch…nge announcement Colours[Player]”
I am trying to understand string.gsub and I found this code and edited it for my own example, before the game is run the text says: “Change announcement Colours[Player]” and once the game is run I require player to be substituted with “…” and later intend on using this knowledge to create a system that changes [player] to the LocalPlayer Name. What has gone wrong here? Any help is appreciated, thank you.
local TextLabel = script.Parent.Text
local stringtoprocess = TextLabel..""
local stringtofind = "[Player]"
local startvalue,endvalue = string.find(stringtoprocess,stringtofind)
local firststring = string.sub(stringtoprocess,1,startvalue-1)
local secondstring = string.sub(stringtoprocess,endvalue+1,string.len(stringtoprocess))
local stringtoinsert = "..."
local processedstring = firststring..stringtoinsert ..secondstring
script.Parent.Text = processedstring
How would I apply this so it will replace [Player] with the game.players.LocalPlayer.Name) whilst keeping the rest of the text?
I’m assuming I stop the repetition of the string with a debounce.
Escape them or use the fourth argument of string.find to fix the problem.
[Player] would become %[Player%] for what you’re trying to find.
local stringtoprocess = script.Parent.Text
local stringtofind = "%[Player%]"
local startvalue,endvalue = string.find(stringtoprocess,stringtofind)
local firststring = string.sub(stringtoprocess,1,startvalue-1)
local secondstring = string.sub(stringtoprocess,endvalue+1,#stringtoprocess)
local stringtoinsert = "..."
local processedstring = firststring..stringtoinsert ..secondstring
script.Parent.Text = processedstring