Darkrp Arrest baton: changing colors on use.

Hi, I am trying to get the Darkrp Arrest baton to change colors upon using. I have tried many ways to attempt at doing this. If anyone could help me that would be great. It has a material so conventional setcolor or setweaponcolor doesn’t work. This was my attempt at fixing it:


function SWEP:Initialize(wasInitialized)
    if (wasInitialized ~= true) then
    self:SetHoldType("normal")

    self.stickRange = 90
    if SERVER then return end

    stunstickMaterials = stunstickMaterials or {}


    local materialName = "darkrp/" .. self:GetClass()
    if stunstickMaterials[materialName] then return end

    CreateMaterial(materialName, "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color2", self.StickColor:ToVector())
    else
    if SERVER then return end
    stunstickMaterials = stunstickMaterials or {}


    local materialName = "darkrp/" .. self:GetClass()
    if stunstickMaterials[materialName2] then return end
    CreateMaterial(materialName2, "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color2", Color(127,127,127,127):ToVector())

    stunstickMaterials[materialName2] = true
    self.wasInitialized = true
    end
end

// it is called with BaseClass.Initialize(true)


and this is the error:


[ERROR] gamemodes/darkrp/entities/weapons/stick_base/shared.lua:40: attempt to index local 'self' (a boolean value)  1. Initialize - gamemodes/darkrp/entities/weapons/stick_base/shared.lua:40   2. unknown - gamemodes/darkrp/entities/weapons/arrest_stick/shared.lua:94

When you’re calling BaseClass.Initialize, put what you’re trying to use as self as the first argument, like this
[lua]BaseClass.Initialize( self, true )[/lua]
Assuming you’re calling this in the weapon’s initialize function.

That worked, thanks. The color of the baton is still unchanged though,

http://forum.facepunch.com/showthread.php?t=1338430

The DarkRP arrest baton already has a set material. Nothing given in that topic is working.

materialName2 isn’t defined, and when you define it make sure you give it a different name from the first.

Also you have this
[lua]if SERVER then return end
stunstickMaterials = stunstickMaterials or {}[/lua]
two times, which is redundant.

The first time was within an if statement, the second was in the else statement. I thought repetition would be necessary. Also I defined materialName2, still nothing


function SWEP:Initialize(wasInitialized)
    if (wasInitialized ~= true) then
    self:SetHoldType("normal")

    self.stickRange = 90
    if SERVER then return end

    stunstickMaterials = stunstickMaterials or {}


    local materialName = "darkrp/" .. self:GetClass()
    local materialName2 = "darkrp/" .. self:GetClass()
    if stunstickMaterials[materialName] then return end

    CreateMaterial(materialName, "VertexLitGeneric", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color2", self.StickColor:ToVector())
    else
    if SERVER then return end
    local materialName2 = "darkrp/" .. self:GetClass()
    stunstickMaterials = stunstickMaterials or {}


    local materialName = "darkrp/" .. self:GetClass()
    if stunstickMaterials[materialName2] then return end
    CreateMaterial(materialName2, "VertexLitGeneric2", {
        ["$basetexture"] = "models/debug/debugwhite",
        ["$surfaceprop"] = "metal",
        ["$envmap"] = "env_cubemap",
        ["$envmaptint"] = "[ .5 .5 .5 ]",
        ["$selfillum"] = 0,
        ["$model"] = 1
    }):SetVector("$color2", Color(127,127,127,127):ToVector())

    stunstickMaterials[materialName2] = true
    self.wasInitialized = true
    end
end

I didn’t notice the else, my bad.

CreateMaterial won’t recreate the material if it already exists, have you tried changing the material name or restarting your game?

The else statement is there so when it initializes normally (if (wasInitialized ~= true)) it won’t get the effects of the else statement. Also I have tried restarting the game,

CreateMaterial( string name, string shaderName, table materialData )

that is the syntax. materialName and materialName2 are each unique string names. I want everything about everything to be the same except the color of it which is why the

SetVector("$color2", Color(127,127,127,127):ToVector())

is the only difference between them. I have also defined local materialName2 = “darkrp/” … self:GetClass()
it is exactly the same is 1 because i didn’t think any difference in that was necessary as only the colors are supposed to change.

[editline]23rd August 2017[/editline]

Now what i’ve tried doing was changing the self.StickColor in the function then running self.Initialize(self) but the color still is not changing. I am at a bit of a loss.