Hi. I've been trying for the last couple of hours to try and change a part of a model path but cannot figure it out. If anyone would be so kind and help me or point me in the direction of how to do it it would be very appreciated.
I want to get the players model and change it by only changing the "Group01" to "Group03", so that whatever playermodel I use the entity will always change and keep the playermodels face.
Example:
(Both using the same entity)
"models/Humans/Group01/Male_04.mdl" to "models/Humans/Group03/Male_04.mdl"
or
"models/Humans/Group01/Female_04.mdl" to "models/Humans/Group03/Female_04.mdl"
Use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/gsub]string.gsub[/url]
I was able to do this, but I don't know how to do the last "Male_04.mdl", so that I can be any model and keep it but change group.
player:SetModel(string.gsub( "models/Humans/Group01/Male_04.mdl", "Group01", "Group03" ))
local group = "Group01"
player:SetModel(player:GetModel():gsub("models/Humans/(.*)/","models/Humans/" .. group .. "/"))
--or
player:SetModel("models/Humans/" .. group .. "/Male01")
An easy way of doing it would involve using [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/string/Split]string.Split[/url] and checking if splitstring[3] is "Group01" then change that and splitstring[4] to what you'd like.
local path = "models/Humans/Group01/Male_04.mdl"
local split = path:Split("/")
if (split[3] == "Group01") then
split[3] = "Group03"
split[4] = "Male_01.mdl"
end
path = table.concat(split, "/")
Didn't solve the problem, but worked the same way this did:
player:SetModel(string.gsub( "models/Humans/Group01/Male_04.mdl", "Group01", "Group03" ))
Kevlon, I don't understand what your code does exactly. Would you mind explaining?
local group = "Group01"
player:SetModel(player:GetModel():gsub("models/Humans/(.*)/","models/Humans/" .. group .. "/"))
--or
player:SetModel("models/Humans/" .. group .. "/Male01")
You would have to change it with your own logic, but the top one would allow you to change peoples models easily
It's working fine for me. What exactly do you want?
print(path) -- models/Humans/Group01/Male_04.mdl
path = table.concat(split, "/")
print(path) -- models/Humans/Group03/Male_01.mdl
If you want to check if the path ends with Male_04.mdl you just check splitstring[4] instead of splitstring[3].
local path = "models/Humans/Group01/Male_04.mdl"
local split = path:sub(1, -5):Split("/")
if (split[4] == "Male_04") then
split[4] = "Male_01"
end
print(path) -- "models/Humans/Group01/Male_04.mdl"
path = table.concat(path, "/") .. ".mdl"
print(path) -- "models/Humans/Group01/Male_01.mdl"
I'm not sure how to explain it any better. Ill try.
I have an entity that changes the players model. I want the player to be able to have "Female01", "Female02", "Male05", "Male 08" and so on, whatever their model ends with, I want them to be able to use the same entity to change their modelpath from "Group01" to "Group03".
Is this better? I can send a LONG code of an example on how it could be done with alot of repetitive code.
Then you could just use a table to check the final path segment and have it as a replacement.
local path = "models/Humans/Group01/Male_05.mdl"
local split = path:sub(1, -5):Split("/")
local Replacements = {
Female01 = true,
Female02 = true,
Male05 = true,
Male08 = true,
}
if (Replacements[split[4]]) then
split[3] = "Group03"
end
print(path) -- "models/Humans/Group01/Male_05.mdl"
path = table.concat(path, "/") .. ".mdl"
print(path) -- "models/Humans/Group03/Male_05.mdl"
I can't get it to work :/
No errors or anything..
Did his test code print correctly for You? You should probably just convert all the path elements to lowercase, as well.
No, no print, I'll give it a try!
The table.concat line should be:
path = table.concat(split, "/") .. ".mdl"
It now prints like it should and no error, but nothing happens to the model.
The table entries don't contain an underscore between the gender and number like the path does. Really, Kevlon's solution was the most efficient way to approach it.
I'll try that one instead then.
Let him figure that sort of stuff out on his own. We can't expect people to learn if we keep giving them the answers.
Did this, and no error but nothing happens to the playermodel
local group = "Group03"
item.player:SetModel(item.player:GetModel():gsub("models/Humans/(.*)/","models/Humans/" .. group .. "/"))
Because you're not changing it. Take a look at patterns and how they work.
I completely agree ideologically, but this was from a typo made in the code given to him, and, in my eyes, hes clearly putting effort into understanding and implementing the code into his application.
Sorry, you need to Log In to post a reply to this thread.