Replacing items in a string

I have quick phrases on a radio, with items like “LOCATION” and “CALLSIGN”, both of which I have the variables for, I’m just wondering on how to actually replace them?

Eg: “Show CALLSIGN 10-8 at LOCATION” would be “Show 1P23 10-8 at Spawn”

I don’t think I can use Gsub, one of my other methods was to use :split() and then loop through them and change depending on what it says, but thats not really efficient, so was gonna ask you guys.

Thank you all in advance.

1 Like

You can use gusb

local ex = "Hello world" -- this is the string we want to remove the stuff from
local word = 'world' -- this is the string we want to remove from ex
local replace = "" -- this is the string we want to replace the word(s) with

ex = string.gsub(ex, word, replace)
print(ex) -> Hello

Also for the word, it can actually be as many words, phrases, etc as you like.
if you leave replace as “” or blank, it will basically just remove it,
but if replace was like “Tomato” ex would print Hello Tomato.

1 Like

Would it throw an error if the item is not in the string?

1 Like

Hmm I dont think so, you should be good but you can test it out if you want

2 Likes
local CALLSIGN = "1P23"
local LOCATION = "Spawn"

local RadioMessage = "Show CALLSIGN 10-8 at LOCATION"
RadioMessage = RadioMessage:gsub("CALLSIGN", CALLSIGN):gsub("LOCATION", LOCATION)

gsubs works very well here

3 Likes

Will do! Thank you very much gotta fill characters

you can use `` for text and it let you add variables to them

local location = "Roblox"
local callSign = "Good"

print(`{location} is {callSign}`)

Doesn’t seem like it’s working? Will attach result, and code :slight_smile:
image
image

button.MouseButton1Click:Connect(function()
			local correct = Information[button.Name]
			local random = math.random(1, #Information[button.Name])
			local RadioMessage = correct[random]
			RadioMessage = RadioMessage:gsub("CALLSIGN", Callsign):gsub("LOCATION", getLocation())
			SendChatter(Callsign, correct[random], false)
		end)

nevermind im an idiot :sob:

you’re modifying the variable RadioMessage, but then sending correct[random] (the original message) to SendChatter

Yeah thats why I added the bottom message :laughing:
Sorry about that!

I didn’t see that lmao, you welcome

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.