Little help with replacing words in Strings

(First time creating a topic here so I wouldn’t be surprised if this is in an incorrect category)

BACKGROUND: So my friends and I are making a fighting game and the game can get pretty chaotic sometimes. Which makes it hard to tell who exactly killed you. So we decided to make killfeed that informs the dead player who killed them.

PROBLEM: Currently it only allows one kill message format, forcing the killer’s name to be at the end of the feed. For example, if Player1 dies to Player2, Player1 will see a text that says, “You were killed by Player2.” However, my friend and I wanted to make it so the killer’s name isn’t always at the end of the line like, “Player2 got you really good there, didn’t he?”

I did some exploring on the toolbox and the devforum for anything relating to what I wanted to achieve. While I was exploring, I found a killfeed script on the toolbox and checked it out, and the script has these lines in it:

local KillPhrases = {
	"\t bloxxed \n",
	"\t sliced \n",
	"\t killed \n",
}

From my understanding, the script replaces \t with the killer’s name and \n with the victim’s name, so I did some diving around the script and probably found what changes the special words inside those kill phrases. The script uses string.sub so Ima assume the method involves with string.sub.

while true do
	Place = Place + 1
	if Place > PhraseLen then
		break
	end
	local _char_ = string.sub(Phrase,Place,Place)
	if _char_ == "\n" then
		if Place > StartPlace then
			table.insert(Chunks,string.sub(Phrase,StartPlace,Place - 1))
		end
		table.insert(Chunks,DefeatedName)
		StartPlace = Place + 1
	elseif _char_ == "\t" then
		if Place > StartPlace then
			table.insert(Chunks,string.sub(Phrase,StartPlace,Place - 1))
		end
		table.insert(Chunks,KillingName)
		StartPlace = Place + 1
	end
end

So my question is does anyone know how to replace a certain word in a string with another word and if anyone does know, can they leave an explanation to me so I can understand how does one does it. (and maybe show an example of it if willing to, not asking for someone to write an entire script, but maybe just a portion of how one does it.)

I’ll gladly appreciate anyone who’s willing to help.

2 Likes

You can use the string indicator %s as a string place holder.

local str = string.format("%s sliced %s", "Bob", "Jim")
print(str) -- "Bob sliced Jim"
4 Likes

Thank you for helping me. I actually thought it was going to be more complex instead of it being a 2 lined code.

3 Likes