Spawn an entity with a vehicle?

Someone could explain me why this shit doesn’t working?

In Autorun

[lua]local function SpawnedVehicle ( player, vehicle )
if player:InVehicle() then
local vehicle = player:GetVehicle()

local class = player:GetVehicle():GetClass();
local model = player:GetVehicle():GetModel();
if(class == “prop_vehicle_jeep” and model == “models/buggy.mdl”) then

     self.gun = ents.Create( "Test" )
    self.gun:SetParent( vehicle )    
    self.gun:SetPos( vehicle:GetPos() + vehicle:GetForward() * 20 + vehicle:GetUp() * 60 + vehicle:GetRight() * 17  )
    self.gun:SetAngles( vehicle:GetAngles() )
    self.gun:Spawn()
    self.gun:Activate()
    end
end

end

hook.Add( “PlayerSpawnedVehicle”, “SpawnedVehicle”, SpawnedVehicle );[/lua]

playerWarthogVehicle isn’t a valid hook. Did you mean **[Gamemode.PlayerSpawnVehicle

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.PlayerSpawnVehicle)**?

i tryied with that[lua] local function SpawnedVehicle ( player, vehicle ) hook.Add( “PlayerSpawnedVehicle”, “SpawnedVehicle”, SpawnedVehicle ); [/lua] it still doesn’t work

A few problems here I’m afraid. But you need to tell us what your goal is with this code and exactly what’s happening, whether or not you’re getting any errors etc.

As for the problems themselves, this isn’t in the code of SWEP or SENT and so self doesn’t exist on lines 9-14. On line 12 you used Vehicle, which should be vehicle. It’s case sensitive.

Your indentation looks quite messy as well, if you fix that we can read your code much more easily.

in fact, when you spawn a buggy and enter in it, It should spawn the test entity and weld it to the buggy(vehicle)

This is what i’m trying to make

If you want it to spawn the entity when you enter the vehicle, what you’re looking for is a PlayerEnteredVehicle hook. eg

[lua]
local function JeepAttachEnt(ply, ent, role)
if ent:GetClass() == “prop_vehicle_jeep” and ent:GetModel() == “models/buggy.mdl” then
–spawn testent
end
end
hook.Add(“PlayerEnteredVehicle”, “JeepAttachTestEnt”, JeepAttachEnt)
[/lua]

what about i want to delete it without deleting the car?

self.gun:Remove()