Jump to content

Recommended Posts

Currently making simple "go slightly transparent" toggle action, keep getting error attempt to call global 'GetTime' (a nil value) whenever using right click. Any help with improving the code? Thanks in advance!

Modmain:

local function MakeSemiTransparent(act)
    local inst = act.doer
    if inst ~= nil and inst.Transform ~= nil then
        inst.AnimState:SetMultColour(1, 1, 1, 0.5)

        inst.last_transparent_time = GetTime()

        inst:DoTaskInTime(3, function()
            if inst.AnimState ~= nil then
                inst.AnimState:SetMultColour(1, 1, 1, 1)
            end
        end)

        return true
    end
end

AddAction("SEMI_TRANSPARENT", "Semi-Transparent", MakeSemiTransparent)
_G.ACTIONS.SEMI_TRANSPARENT.distance = math.huge
_G.ACTIONS.SEMI_TRANSPARENT.instant = true

Character prefab:

local function CanGoTransparent(inst)
        return (GetTime() - inst.last_transparent_time > inst.transparent_cooldown)
end

local function GetPointSpecialActions(inst, pos, useitem, right)
	if right and CanGoTransparent(inst) then
       return { ACTIONS.SEMI_TRANSPARENT }
    end
    return {}
end

local function OnSetOwner(inst)
    if inst.components.playeractionpicker ~= nil then
       inst.components.playeractionpicker.pointspecialactionsfn = GetPointSpecialActions
    end
end

Common-postinit:

inst:ListenForEvent("setowner", OnSetOwner)
inst.last_transparent_time = 0
inst.transparent_cooldown = 5

 

Posted (edited)
On 5/13/2026 at 3:07 AM, RexySeven said:

Currently making simple "go slightly transparent" toggle action, keep getting error attempt to call global 'GetTime' (a nil value) whenever using right click. Any help with improving the code? Thanks in advance!

Modmain:

local function MakeSemiTransparent(act)
    local inst = act.doer
    if inst ~= nil and inst.Transform ~= nil then
        inst.AnimState:SetMultColour(1, 1, 1, 0.5)

        inst.last_transparent_time = GetTime()

        inst:DoTaskInTime(3, function()
            if inst.AnimState ~= nil then
                inst.AnimState:SetMultColour(1, 1, 1, 1)
            end
        end)

        return true
    end
end

AddAction("SEMI_TRANSPARENT", "Semi-Transparent", MakeSemiTransparent)
_G.ACTIONS.SEMI_TRANSPARENT.distance = math.huge
_G.ACTIONS.SEMI_TRANSPARENT.instant = true

Character prefab:

local function CanGoTransparent(inst)
        return (GetTime() - inst.last_transparent_time > inst.transparent_cooldown)
end

local function GetPointSpecialActions(inst, pos, useitem, right)
	if right and CanGoTransparent(inst) then
       return { ACTIONS.SEMI_TRANSPARENT }
    end
    return {}
end

local function OnSetOwner(inst)
    if inst.components.playeractionpicker ~= nil then
       inst.components.playeractionpicker.pointspecialactionsfn = GetPointSpecialActions
    end
end

Common-postinit:

inst:ListenForEvent("setowner", OnSetOwner)
inst.last_transparent_time = 0
inst.transparent_cooldown = 5

 

I’m going to use DoTaskInTime or a Timer since I’m more familiar with them. Testing it out in the modmain


local TRANSPARENT_DURATION = 3
local TRANSPARENT_COOLDOWN = 2

local function MakeSemiTransparent(act)
    local inst = act.doer
    if inst ~= nil and inst.Transform ~= nil then
        inst.AnimState:SetMultColour(1, 1, 1, 0.5)
        inst.transparent_active = true
        
        inst.components.timer:StartTimer("transparent_duration", TRANSPARENT_DURATION)

        return true
    end
end

AddAction("SEMI_TRANSPARENT", "Semi-Transparent", MakeSemiTransparent)
_G.ACTIONS.SEMI_TRANSPARENT.distance = math.huge
_G.ACTIONS.SEMI_TRANSPARENT.instant = true

AddPlayerPostInit(function(inst)
    local function CanGoTransparent(inst)
        return not inst.transparent_active and not inst.transparent_oncooldown
    end

    local function GetPointSpecialActions(inst, pos, useitem, right)
        if right and CanGoTransparent(inst) then
            return {ACTIONS.SEMI_TRANSPARENT}
        end
        return {}
    end

    local function OnSetOwner(inst)
        if inst.components.playeractionpicker ~= nil then
            inst.components.playeractionpicker.pointspecialactionsfn =
                GetPointSpecialActions
        end
    end

    inst:ListenForEvent("timerdone", function(inst, data)
        if data.name == "transparent_duration" then
            inst.transparent_active = false
            if inst.AnimState ~= nil then
                inst.AnimState:SetMultColour(1, 1, 1, 1)
            end
            inst.transparent_oncooldown = true
            inst.components.timer:StartTimer("transparent_cooldown", TRANSPARENT_COOLDOWN)

        elseif data.name == "transparent_cooldown" then
            inst.transparent_oncooldown = false
        end
    end)

    inst:ListenForEvent("setowner", OnSetOwner)
    inst.transparent_active = false
    inst.transparent_oncooldown = false
end)



 

Edited by Haruhi Kawaii
  • Like 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
  • Create New...