Prevent players from spawning more than 1 simfphys cars

Hello, Sorry if the title is not really precise.
I created a NPC that spawns a car on the Simfphys base, but I want to prevent the player for spawning more than one car at a time because last time a player spammed the npc, my game just crashed.

Here’s a small part of my code (the code is in lua/entities/npc_car/init.lua):


function ENT:AcceptInput( Name, Activator, Caller )

    if shouldOccur == true then    
        simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector(-1000,1300,320), Angle(0, 0, 0) ) -- Spawn the car using simfphys
        Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )

        shouldOccur = false -- Prevents from spaming
        timer.Simple( 1, function() shouldOccur = true end )
    end
    
end

I tried to remove the previous car spawned by the player using Entity:Remove but I don’t know how to select the entity to remove (and I don’t really know if I can remove simfphys cars using Entity:Remove).

create a variable on the caller and set it to true once the player has spawned his car, before spawning check if said variable is true.

And how can I remove the previous car ?

When the entity is made, you’re going to want to check if a value is not true;


if not ply.SpawnedSimfphysVehicle then

and then set the variable on the player to true


ply.SpawnedSimfphysVehicle = true

This way they will only be able to spawn one vehicle. Ofcourse if the player deletes the vehicle or it gets removed, then they still wont be able to spawn one. Perhaps you could use something like this:



function HasSpawnedSimfphysVehicle(ply, class)
        for k,v in ipairs(ents.FindByClass(class)) do
             if v:GetOwner() == ply then
                  return true
             end
        end
        return false
end


Where do I need to place this code ?

Wherever the entity is being spawned.

You would implement it like so:



function <whateverthehellitis>(ply)
     if HasSpawnedSimfphysVehicle(ply, "sim_fphys_bak_taxi") then return end

     --Rest of the functions code
end


Assuming your function is correct (the one you posted - I don’t have simfphys so can’t test)



function ENT:AcceptInput( Name, Activator, Caller )
        if HasSpawnedSimfphysVehicle(Caller, "sim_fphys_bak_taxi") then return end

        simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector(-1000,1300,320), Angle(0, 0, 0) ) -- Spawn the car using simfphys
        Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )
end


Is what I’ve done right ? Sorry if I’m retard


function HasSpawnedSimfphysVehicle( ply, class )
        for k,v in ipairs( ents.FindByClass( class ) ) do
             if v:GetOwner() == ply then
                  return true
             end
        end
        return false
end

function ENT:AcceptInput( Name, Activator, Caller )

    if shouldOccur == true then
        if HasSpawnedSimfphysVehicle( Caller, "sim_fphys_bak_taxi" ) then return end -- I need some time to understand how this works, calls the HasSpawnedSimfphysVehicle function

        if Caller.SpawnedSimfphysVehicle == false then
            simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector( 200, -200, 200 ), Angle( 0, 0, 0 ) ) -- Spawns the simfphys car
            Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )
            Caller.SpawnedSimfphysVehicle = true
        else
            Caller:PrintMessage( HUD_PRINTTALK, "You already have a car." )
        end
        
        shouldOccur = false
        timer.Simple( 1, function() shouldOccur = true end )
    end

end

Change ply to Caller inside the AcceptInput function.

The problem is I can’t respawn a new car after removeing my previous one.

I edited my previous post.

You were closer to being correct prior to your edit. that’s all you had to was change what I said

That is because people doesn’t even read what you are asking for, what you are really trying to do. So things get mess up.


        if Caller.SpawnedSimfphysVehicle == false then
            simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector( 200, -200, 200 ), Angle( 0, 0, 0 ) ) -- Spawns the simfphys car
            Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )
            Caller.SpawnedSimfphysVehicle = true
        else
            Caller:PrintMessage( HUD_PRINTTALK, "You already have a car." )
        end

Here you set “Caller.SpawnedSimfphysVehicle” to true for once but you never set it back to false. So when you spawn a car, you wont be able to spawn again as you mentioned it. What you have to do is remove that “if Caller.SpawnedSimfphysVehicle == false then” check since you already checking if the player has a car.

So do I need to do something like this in the AcceptInput function ?


        for k,v in ipairs( ents.FindByClass( "sim_fphys_bak_taxi" ) ) do -- For each entity with the class "sim_fphys_bak_taxi" do
            if v:GetOwner() == Caller then -- If the owner of the entity is the same as the caller then
                Caller:PrintMessage( HUD_PRINTTALK, "You already have a car." )
            elseif v:GetOwner() != Caller then -- Else if the owner of the entity isn't the same as the caller
                simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector( 200, -200, 200 ), Angle( 0, 0, 0 ) )
                Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )
            end
        end


function ENT:AcceptInput( Name, Activator, Caller )

    if shouldOccur == true then
        if not IsValid( Caller.Car ) then
                Caller.Car = simfphys.SpawnVehicleSimple( "sim_fphys_bak_taxi", Vector(-1000,1300,320), Angle(0, 0, 0) ) -- Spawn the car using simfphys
                Caller:PrintMessage( HUD_PRINTTALK, "Your car appears." )
        else
                Caller:PrintMessage( HUD_PRINTTALK, "u already habe car" )
        end

        shouldOccur = false -- Prevents from spaming
        timer.Simple( 1, function() shouldOccur = true end )
    end
    
end


untested

Thanks this works !

I probably explain myself badly