Pivity

Need help with creating a swep

Last read Never
#1

For the past week I've been trying to figure out how to make a "swep" that toggles notarget on and off for the person using it. I wanted to make it to where 1 click turns on notarget and another click turns it off but unfortunately I have failed and I am here to ask if anyone knows how to do this or if this is even possible.

#2

Could you give us a screenshot of your code? Just so we can see what you've tried and maybe improve it.

Agree x 1Winner x 1
#3

if ( SERVER ) then

SWEP.Weight = 5
SWEP.AutoSwitchTo = true
SWEP.AutoSwitchFrom = true

end

if( CLIENT ) then

SWEP.PrintName = "Notarget"
SWEP.Slot = 1
SWEP.DrawAmmo = false
SWEP.DrawCrosshair = false

end

-- Main -- SWEP.Author = "" SWEP.Contact = "" SWEP.Category = "Other" SWEP.Instructions = "Left Click to Enable Notarget/ Disable Notarget." SWEP.Spawnable = true SWEP.AdminOnly = true SWEP.AutoSwitchTo = true SWEP.AutoSwitchFrom = true SWEP.HoldType = "normal" SWEP.ViewModelFOV = 60 SWEP.ViewModelFlip = false SWEP.ViewModel = "models/weapons/c_slam.mdl" SWEP.WorldModel = "" SWEP.UseHands = true

--- PRIMARY FIRE --- function SWEP:PrimaryAttack() if ( !self.notarget ) then self.Owner:SetNoTarget(true) else self.Owner:SetNoTarget(false) end

end

function SWEP:SecondaryAttack() end

function SWEP:notargetoff() self.Owner:SetNoTarget(false) end

function SWEP:OnDrop() self.Owner:SetNoTarget(false) end

Keep in mind that I have almost 0 experience in making anything so my codes are going to be really bad.

Agree x 1
#4

First of all the function you're using ( Player:SetNoTarget() aka self.Owner:SetNoTarget() ) which is a Server-side function, you've seen if SERVER then end or if CLIENT then end, these 2 checks make sure something gets only loaded on server-side or only on client-side. Everything outside these 2 checks is called shared ( both client and server, this only works when the file is ran shared aswell ). Calling self.Owner:SetNoTarget() will work for the server but the client will get an error, to prevent this we use if SERVER then end yet again.

Also do you check if the player has notarget on him using self.notarget. First thing, self.notarget will call the var on the swep itself and not the player so we need to use self.Owner.notarget. Seccond thing, due to this var not being a default gmod var do we have to update ( give it content ) the var ourself in order to check if someone actually has notarget.

I suggest this:

if ( SERVER ) then

SWEP.Weight = 5

SWEP.AutoSwitchTo = true

SWEP.AutoSwitchFrom = true

end

if( CLIENT ) then

SWEP.PrintName = "Notarget"

SWEP.Slot = 1

SWEP.DrawAmmo = false

SWEP.DrawCrosshair = false

end

-- Main --

SWEP.Author = ""

SWEP.Contact = ""

SWEP.Category = "Other"

SWEP.Instructions = "Left Click to Enable Notarget/ Disable Notarget."

SWEP.Spawnable = true

SWEP.AdminOnly = true

SWEP.AutoSwitchTo = true

SWEP.AutoSwitchFrom = true

SWEP.HoldType = "normal"

SWEP.ViewModelFOV = 60

SWEP.ViewModelFlip = false

SWEP.ViewModel = "models/weapons/c_slam.mdl"

SWEP.WorldModel = ""

SWEP.UseHands = true

--- PRIMARY FIRE ---

function SWEP:PrimaryAttack()

if SERVER then -- Making sure this only gets called from the server-side

if ( !self.Owner.notarget ) then -- Calling the var from the player

self.Owner:SetNoTarget(true)

self.Owner.notarget = true -- updating the var

else

self.Owner:SetNoTarget(false)

self.Owner.notarget = false -- updating the var again

end

end

end

function SWEP:SecondaryAttack()

end

function SWEP:OnRemove() -- Also added this to remove the var when the weapon gets removed.

if SERVER then

self.Owner:SetNoTarget(false)

self.Owner.notarget = false -- updating the var

end

end

function SWEP:OnDrop()

if SERVER then

self.Owner:SetNoTarget(false)

self.Owner.notarget = false -- updating the var
end

I hope this explains it to you, if not you can always contact me on steam for more questions.

Users viewing this thread
Nobody