NEED HELP! Newbie Scripter here

Alright

I’m fairly new to Lua and coding in general. I’ve had a stab at it before but never really got engaged until now.

I’m working on a STool with the following functions:
Right Click: Gets the material path of target entity.
Left Click: Sets this material on other target entities, rather like the materials STool.
Reload: Resets the new overrides to default.

I’m having a lot of trouble around the “getting the material path” part, as you might expect. I’ve looked at http://wiki.garrysmod.com/?title=Entity.GetMaterial but can’t understand it.

Here’s my code (edits of the default material.lua file)
[lua]
TOOL.Category = “Render”
TOOL.Name = “#Velikoth’s Material Grabber”
TOOL.Command = nil
TOOL.ConfigName = “”

TOOL.ClientConVar[“entmat”] = “debug/env_cubemap_model”

local function SetMaterial( Player, Entity, Data )

Entity:SetMaterial( Data.MaterialOverride )

if ( SERVER ) then
	duplicator.StoreEntityModifier( Entity, "material", Data )
end

return true

end
duplicator.RegisterEntityModifier( “material”, SetMaterial )

function TOOL:LeftClick( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

local mat = self:GetClientInfo( "entmat" )
SetMaterial( self:GetOwner(), trace.Entity, { MaterialOverride = mat } )
return true

end

function TOOL:RightClick(trace)
if( !CLIENT ) then return end
if !( trace.Entity && // Hit an entity
trace.Entity:IsValid() && // And the entity is valid
trace.Entity:EntIndex() != 0 // And isn’t worldspawn
) then return end

	entmat = ent:GetMaterial()

end

function TOOL:Reload( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

SetMaterial( self:GetOwner(), trace.Entity, { MaterialOverride = "" } )
return true

end[/lua]

I’ve been told its a logic error but I don’t know what the hell that means.

I’m guessing the problem comes from not using the Entity:GetMaterial() properly, but the gmod lua wiki has really bad documentation on learning to use these commands! Especially for a noob like me :frowning:

Please help me, guys.

Bump

[lua]
TOOL.Category = “Render”
TOOL.Name = “#Velikoth’s Material Grabber”
TOOL.Command = nil
TOOL.ConfigName = “”

TOOL.ClientConVar[“entmat”] = “debug/env_cubemap_model”

local function SetMaterial( Player, Entity, Data )

Entity:SetMaterial( Data.MaterialOverride )

if ( SERVER ) then
	duplicator.StoreEntityModifier( Entity, "material", Data )
end

return true

end
duplicator.RegisterEntityModifier( “material”, SetMaterial )

function TOOL:LeftClick( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

local mat = self:GetClientInfo( "entmat" )
SetMaterial( self:GetOwner(), trace.Entity, { MaterialOverride = mat } )
return true

end

function TOOL:RightClick(trace)
if( !CLIENT ) then return end
if !( trace.Entity && // Hit an entity
trace.Entity:IsValid() && // And the entity is valid
trace.Entity:EntIndex() != 0 // And isn’t worldspawn
) then return end

	print(trace.Entity:GetMaterial())

end

function TOOL:Reload( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

SetMaterial( self:GetOwner(), trace.Entity, { MaterialOverride = "" } )
return true

end[/lua]

Doesn’t work.

And I don’t want to print it to console, I want to be able to set that object’s material on other entities.

Well explain more of what you want instead of

Explain what you want it to do and i will fix it.

Okay, using my mighty technical terminology:

Right Click would get the material path of the target entity and store that so it can be applied to other entities with Left Click.

EG: Right clicks on a Door and gets that door’s material path.
Left clicks on a bench and sets the bench’s material as that of the door targetted with Right Click.

[lua]TOOL.Category = “Render”
TOOL.Name = “#Velikoth’s Material Grabber”
TOOL.Command = nil
TOOL.ConfigName = “”

local default = “debug/env_cubemap_model”
local mat = mat or default

function TOOL:LeftClick( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

trace.Entity:SetMaterial( mat )

return true

end

function TOOL:RightClick(trace)
if( !CLIENT ) then return end
if !( trace.Entity && // Hit an entity
trace.Entity:IsValid() && // And the entity is valid
trace.Entity:EntIndex() != 0 // And isn’t worldspawn
) then return end

	self.Owner:ChatPrint("Got Material ".. mat)
	mat = trace.Entity:GetMaterial()

end

function TOOL:Reload( trace )

if !( trace.Entity &&			// Hit an entity
      trace.Entity:IsValid() && 	// And the entity is valid
      trace.Entity:EntIndex() != 0	// And isn't worldspawn
    ) then return end

trace.Entity:SetMaterial( mat )

return true

end[/lua]

Isn’t working.
Right click doesn’t do anything and Left click sets the material as cubemap.

Because there is no material set on the door ect the GetMaterial gets the material from the .vtf not the model material that’s all ready on there.

:GetMaterial() is used for getting the material previously set using :SetMaterial().

It doesn’t get the true material of the model, it gets the material override.

Oh, I see.

So it isn’t possible?

Well not unless the material is set first.