Need some help with my code.

Hey guys, I am an armature lua coder and I have been trying to make buy able pets for the pointshop on my server.

Here is the code for it:


ITEM.Name = 'Fast Headcrab'
ITEM.Price = 1000
ITEM.Model = 'models/headcrab.mdl'
ITEM.SingleUse = false

function ITEM:OnEquip(ply)

	pet = ents.Create("npc_headcrab_fast")
	pet:SetOwner(ply)
	
	pet:SetMaxHealth(150)
	pet:SetHealth(150)
	
	pet:AddEntityRelationship(ply, 3, 99)
	
	 for k,v in pairs(player.GetAll()) do
		if(v != ply) then
			pet:AddEntityRelationship(v, 4, 99)
		end
    end
	
	pet:SetPos(ply:GetPos()+Vector(0,0,10))
	pet:SetCollisionGroup(COLLISION_GROUP_WORLD)
	
	
	
	pet:Spawn()
	pet:Activate()
	checkpet = 1
	timer.Create("Pet_Follow", 0.1, 0, Follow, ply)
	
end

function Follow(ply)
if checkpet == 1 then
if pet:IsValid() then
pet:SetLastPosition(pet:GetOwner():GetPos())
pet:SetSchedule(71)
end
end
end




function ITEM:OnHolster(ply)
	if checkpet == 1 then
	if pet:IsValid() then
	
	pet:Remove()
	end
	end
	
end

It spawns the headcrab fine but it doesn’t set its health. Also, if someone else buys the headcrab while I (or anyone else) has it equipped, it will stop following me and if they holster it while someone else’s headcrab is out, it disappears.

I am also trying to make a Double Jump power up, here is its code:


ITEM.Name = 'Double Jump'
ITEM.Price = 600
ITEM.Model = 'models/Items/battery.mdl'
ITEM.NoPreview = false

function ITEM:OnEquip(ply, modifications)


	end 
	
	function ITEM:Think(ply)
	if ply:Health() >=1 then


	if IsValid(ply) then
		



if ply:IsOnGround() then
Jumps=0
end

if ply:KeyPressed(IN_JUMP) then 

Jumps = (Jumps + 1)
if Jumps==2 then
local ang = ply:GetAngles()
local forward, right = ang:Forward(), ang:Right()

local vel = -1 * ply:GetVelocity() -- Nullify current velocity
vel = vel + Vector(0, 0, 300) -- Add vertical force

local spd = ply:GetMaxSpeed()

if ply:KeyDown(IN_FORWARD) then
vel = vel + forward * spd
elseif ply:KeyDown(IN_BACK) then
vel = vel - forward * spd
end

if ply:KeyDown(IN_MOVERIGHT) then
vel = vel + right * spd
elseif ply:KeyDown(IN_MOVELEFT) then
vel = vel - right * spd
end

ply:SetVelocity(vel)
end
end
end
end
end 

function ITEM:OnHolster(ply)

end

 

This has a similar problem where if two people are wearing it it doesn’t work properly. You both have to jump at the same time and then only one of you can jump until the other lands on the ground.

I understand why I get these problems, I am just clueless on how to fix them.

Not sure, but make local, not global variables

aAs for jumps, you’re using a global setting for everyone. Instead of Jumps, do ply.Jumps = x

As I said, I am an amateur, how would I do this?

-edit If you mean add local before the variable, it doesn’t work.

prefix local:

local pet = code

Excuse me, armature?

Anyway,



ITEM.Name = 'Fast Headcrab'
ITEM.Price = 1000
ITEM.Model = 'models/headcrab.mdl'
ITEM.SingleUse = false

function ITEM:OnEquip(ply)

    local pet = ents.Create("npc_headcrab_fast")
    pet:SetOwner(ply)
    
    pet:SetMaxHealth(150)
    pet:SetHealth(150)
    
    pet:AddEntityRelationship(ply, 3, 99)
    
    for k,v in pairs(player.GetAll()) do
        if(v != ply) then
            pet:AddEntityRelationship(v, 4, 99)
        end
    end
    
    pet:SetPos(ply:GetPos()+Vector(0,0,10))
    pet:SetCollisionGroup(COLLISION_GROUP_WORLD)

    pet:Spawn()
    pet:Activate()

    timer.Create("Pet_Follow", 0.1, 0, function()
        if IsValid(pet) then
            pet:SetLastPosition(ply:GetPos())
            pet:SetSchedule(71)
        end
    end )

    ply.Pet = pet
    
end

function ITEM:OnHolster(ply)
    if IsValid(ply.Pet) then
        ply.Pet:Remove()
    end
end
    


Only problem with this is it makes it local to the function, which means I couldn’t call it in the holstered function.

Also, code formatting is essential!



ITEM.Name = 'Double Jump'
ITEM.Price = 600
ITEM.Model = 'models/Items/battery.mdl'
ITEM.NoPreview = false

function ITEM:OnEquip(ply, modifications)
end 
	
function ITEM:Think(ply)
	if  !IsValid( ply ) || !ply:Alive() then return end

	if ply:IsOnGround() then
		ply.Jumps=0
	end

	if ply:KeyPressed(IN_JUMP) then 

		ply.Jumps = (Jumps + 1)

		if ply.Jumps==2 then
			local ang = ply:GetAngles()
			local forward, right = ang:Forward(), ang:Right()

			local vel = -ply:GetVelocity() -- Nullify current velocity
			vel = vel + Vector(0, 0, 300) -- Add vertical force

			local spd = ply:GetMaxSpeed()

			if ply:KeyDown(IN_FORWARD) then
				vel = vel + forward * spd
			elseif ply:KeyDown(IN_BACK) then
				vel = vel - forward * spd
			end

			if ply:KeyDown(IN_MOVERIGHT) then
				vel = vel + right * spd
			elseif ply:KeyDown(IN_MOVELEFT) then
				vel = vel - right * spd
			end

			ply:SetVelocity(vel)
		end
	end
end 

function ITEM:OnHolster(ply)
end

Another newby question here, but is the ! before !IsValid( ply ) short for not? Or does it have some other meaning?

It’s the same as not, yes.