Help with replacing map texture for dedicated server?

Hi, everyone. I’ve been scouring search engines for help with coding this, but the results are all from years ago, so I think they might be obsolete now. Any help is appreciated! :slight_smile:

I’m trying to take a material texture that is built into a map and write up a script to replace that loaded texture with another one. I would just recompile the map, but what I’m changing is a portrait that would need to be different for every round… so code that can replace the texture on the server map is ideal.

My server loads up a collection of addons and is set to autodownload them, so I’ve made sure to remove this map from the collection and autodownload list so that it doesn’t overwrite my changes with the online version. I’ve tried this so far in lua/autorun/server but even though the server console detects the file, it doesn’t seem to change anything.

The file paths for the materials I’m trying to replace came directly from VIDE, and they match the path for the file in the mat_texture_list command.
The files that I’m trying to replace the materials with are in both the garrysmod/materials folder and also in the garrysmod/materials/hopespeak folder. There are no typos in the file paths or filenames.


--[[ Replace Character Portraits ]]--
if SERVER then
	resource.AddFile( "dangansonia.vmt" )
	resource.AddFile( "dangansonia.vtf" )
	resource.AddFile( "dangansoniavote.vmt" )
	resource.AddFile( "dangansoniavote.vtf" )
else
	local mat = Material ( "hopespeak/danganasahina" )
	local met = Material ( "dangansonia" )
	mat:SetTexture ( "$basetexture", met:GetTexture( "$basetexture" ))
	
	local mat = Material ( "hopespeak/danganasahinavote" )
	local met = Material ( "dangansoniavote" )
	mat:SetTexture ( "$basetexture", met:GetTexture( "$basetexture" ))
end

There are certain things that you can’t do with materials until the first rendering hook has been called. This may be one of them. Try putting the code into a render hook (but make sure it only runs once, as it only needs to run once, and will drop your FPS if you run it continuously).

I have a texture replacing, and can say that it will be enough to put textrure replacing in

GM:PostRender and make it execute once.

Example of making render function do once.



local PostRenderExec = false
hook.Add("PostRender","PostRender0",function()
    if PostRenderExec  == true then return end
    PostRenderExec  = true


        -- There you put material replacement

end)


Edit:

Also, you can write this after replacing:


 hook.Remove("PostRender","PostRender0") 

Or:



if PostRenderExec then return end