• string.Replace not working?
    6 replies, posted
This shouldn't be happening, this is driving me insane!!! function ENT:ReadClass(clas) string.Replace( clas, "npc_", "" ) string.Replace( clas, "_", " " ) Line 317: [2] = ( "Mmmm, " .. self:ReadClass(clas) .. "'s blood!" ) Which is used to print to chat: Judy Rudyard Sawblade: Mmmm, npc_barney's blood! To read more about the weapon involved here, I've placed a spoiler. It's a sawblade gun. Each sawblade has a procedurally generated name, I guess... just lists of names and it randomly grabs a first and last name. It prints this to chat when a sawblade "touches" something. Well, it's the second possible message. Each sawblade usually maintains its name, but if it forgets its name it gets a new name and reports that to chat as if the sawblade suffered a concussion. You can pick them back up if you're not already full on ammo for the gun. The message *should* be "Judy Rudyard Sawblade: Mmmm, barney's blood!" It's reporting an error to me when it can't find this string in my table of response names, which I've fixed temporarily with this: if IsValid(ClassTable[clas]) then toReturn = ClassTable[clas] end [ERROR] addons/garryslifesource_test/lua/entities/ddl_pr_sawblade.lua:317: attempt to concatenate a nil value   1. unknown - addons/garryslifesource_test/lua/entities/ddl_pr_sawblade.lua:317 I know all of this code isn't really needed, but I don't want to abandon it for this reason. It used to work perfectly fine but now all of the sudden it's not. Please help
function ENT:ReadClass(clas) clas = string.Replace( clas, "npc_", "" ) clas = string.Replace( clas, "_", " ")
You're not returning anything in ENT:ReadClass and you can't edit a string like you can with tables so doing what you're doing won't work at all. You'd have to do what sleeppyy posted and you'd also have to return the new variable in the ENT:ReadClass function.
I am returning in ENT:ReadClass. Like I said, it all already worked, but all of the sudden it doesn't anymore. How much more code do you want?
Strings in Lua are immutable and have always been. Look up how the function works on the Wiki, the example literally uses it as a return value string.Replace
The problem is, it really did used to work how I had it written. You claim it's always been as it is now, but that suggests what I did never should have worked. That's a paradox. It works now, though, so this question is answered. Thanks for the help
Lua refreshes can be a pain sometimes. Old code could still be in memory running, potentially causing what you're experiencing.
Sorry, you need to Log In to post a reply to this thread.