Author Topic: How to make a toggle in ULX  (Read 3786 times)

0 Members and 2 Guests are viewing this topic.

Offline imSEnsE

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
How to make a toggle in ULX
« on: May 05, 2018, 11:27:20 AM »
Hey! I have been working on this add-on, so if i pick up a player with my physgun i can freeze them.. I'm done with that.. But I want to make a ULX command to toggle it.

But I am kinda stuck. How do i do this?

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: How to make a toggle in ULX
« Reply #1 on: May 05, 2018, 01:24:23 PM »
Though we're more than happy to help people code, you do realize some of the ability you speak of is included in ULX, correct?
https://github.com/TeamUlysses/ulx/blob/ecd205e771bdbe7be6a09dea7567ac33eca1fc12/lua/ulx/modules/sh/util.lua#L455
Admins and Players with "ulx physgunplayer" can use physgun other players and freeze them (if IsSandbox according to our checks)

Now, on to your own project.
Where exactly are you getting stuck?

What exactly are you trying to toggle?
Are you trying to toggle the ability of the physgun to freeze a player or not?
Or are you trying to toggle the person(s) freeze state after they've been frozen?

"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline imSEnsE

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: How to make a toggle in ULX
« Reply #2 on: May 07, 2018, 01:33:05 PM »
I have made the add-on, but i just have an new update idea which i am little confused on... I wan't to make a command that toggles the add-on, that i've made.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: How to make a toggle in ULX
« Reply #3 on: May 07, 2018, 07:41:44 PM »
https://github.com/TeamUlysses/ulx/blob/ecd205e771bdbe7be6a09dea7567ac33eca1fc12/lua/ulx/modules/sh/util.lua#L171
Good example of a toggle. For us, it allows noclip for a player, or disables.
In the example code, it passes a single player, or multiple, in a table #target_plys.
PlayersArg

You could do single only with PlayerArg.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline imSEnsE

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: How to make a toggle in ULX
« Reply #4 on: July 03, 2018, 08:06:27 AM »
I see how this might work. In the moment of typing this I am opening Garry's Mod.

Offline imSEnsE

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: How to make a toggle in ULX
« Reply #5 on: July 03, 2018, 08:49:00 AM »
I see how this might work. In the moment of typing this, I am opening Garry's Mod.

Nope... Didn't work... Was some random error, idk what it said completely but I saw some "Ulib.cmds.Translate--something" idk.

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: How to make a toggle in ULX
« Reply #6 on: July 03, 2018, 04:33:15 PM »
Without seeing your code and the exact error, we're throwing pillows at elephants to try to fix a leaky pipe.
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming

Offline imSEnsE

  • Newbie
  • *
  • Posts: 6
  • Karma: 0
Re: How to make a toggle in ULX
« Reply #7 on: July 05, 2018, 03:52:32 PM »
Here's the code, and the Error was something about TranslateCmds I think.


Code: [Select]
--[[Made By SEnsE/Nismo/A. Nismo]]--
if CLIENT then return end; if not SERVER then return end


--[[-----------------Script Here--------Don't Touch!-----------------]]--



hook.Remove( "PhysgunDrop", "ulxPlayerDrop")
local function isPlayer(ent) return (IsValid(ent) && ent.GetClass && ent:GetClass() == "player") end
 
hook.Add("PhysgunPickup", "_ply_physgungrab", function(ply, targ)
    if IsValid(ply) and isPlayer(targ) then
        if ply:query("ulx physgunplayer") then
            local allowed, _ = ULib.getUser( "@", true, ply )
            if isPlayer(allowed) then
                if allowed.frozen && ply:query( "ulx unfreeze" ) then
                    allowed.phrozen = true;
                    allowed.frozen = false;
                end
                allowed._ulx_physgun = {p=targ:GetPos(), b=true}
            end
        end
    end
end, HOOK_HIGH)
 
hook.Add("PlayerSpawn", "_ply_physgungrab", function(ply)
    if ply._ulx_physgun then
        if ply._ulx_physgun.b and ply._ulx_physgun.p then
            timer.Simple(0.001, function()
                ply:SetPos(ply._ulx_physgun.p);
                ply:SetMoveType(MOVETYPE_NONE);
            end);
        end
    end
end)
 
function physgun_freeze( calling_ply, target_ply, should_unfreeze)
    local v = target_ply
    if v:InVehicle() then
        v:ExitVehicle()
    end
 
    if not should_unfreeze then
        v:Lock()
        v.frozen = true
        v.phrozen = true
        ulx.setExclusive( v, "frozen" )
    else
        v:UnLock()
        v.frozen = nil
        v.phrozen = nil
        ulx.clearExclusive( v )
    end
 
    v:DisallowSpawning( not should_unfreeze )
    ulx.setNoDie( v, not should_unfreeze )
 
    if v.whipped then
        v.whipcount = v.whipamt
    end
end


hook.Add("PhysgunDrop", "_ulx_physgunfreeze", function(pl, ent)
    if isPlayer(ent) then
        ent:SetMoveType( MOVETYPE_WALK )
        ent._ulx_physgun = {p=ent:GetPos(), b=false}
    end
 
    if IsValid(pl) and isPlayer(ent) then
        if pl:query("ulx physgunplayer") then
            local isFrozen = ( ent:IsFrozen() or ent.frozen or ent.phrozen );
            ent:SetVelocity(ent:GetVelocity()*-1);
            ent:SetMoveType(pl:KeyDown(IN_ATTACK2) and MOVETYPE_NOCLIP or MOVETYPE_WALK);
            timer.Simple(0.001, function()
                if pl:KeyDown(IN_ATTACK2) and not isFrozen then
                    if pl:query( "ulx freeze" ) then
                        ulx.freeze( pl, {ent}, false );
                        if ent.frozen then ent.phrozen = true end;
                    end
                elseif pl:query( "ulx unfreeze" ) and isFrozen then
                    if pl:KeyDown(IN_ATTACK2) and pl:query( "ulx freeze" ) then
                        physgun_freeze(pl, ent, true)
                        timer.Simple(0.001, function() physgun_freeze(pl, ent, false) end);
                    else
                        ulx.freeze( pl, {ent}, true );
                        if not ent.frozen then ent.phrozen = nil end;
                    end
                end
            end);
        else
            ent:SetMoveType( MOVETYPE_WALK )
        end
    end
end)

local physgunfreeze = ulx.command( Utility, "ulx physgunfreeze", ulx_physgunfreeze, "!physgunfreeze" )
physgunfreeze:addParam{ type=ULib.cmds.PlayersArg, ULib.cmds.optional }
physgunfreeze:defaultAccess( Ulib.ACCES.ADMIN )
physgunfreeze:help( "Toggles Physgun Freeze Player Script" )

print ( "Physgun Player Loaded!" )
--[[Made By SEnsE/Nismo/A. Nismo]]--

Offline JamminR

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 8096
  • Karma: 390
  • Sertafide Ulysses Jenius
    • Team Ulysses [ULib/ULX, other fine releases]
Re: How to make a toggle in ULX
« Reply #8 on: July 05, 2018, 08:42:41 PM »
Seriously, I can't stress this enough - you need to copy paste the exact error from console that you are getting.
It tells what line, it tells what code, and several lines that led to the bad part.

Quick look of your code shows that you should be placing the category Utility in quotes "Utility" - Translate expects a string there, passing it the word Utility likely returns nil, as there is no variable named Utility

Code: [Select]
local physgunfreeze = ulx.command( "Utility", ...)
« Last Edit: July 05, 2018, 08:44:27 PM by JamminR »
"Though a program be but three lines long, someday it will have to be maintained." -- The Tao of Programming