Prevent players from spawning more than 1 simfphys cars
13 replies, posted
[B]Hello,[/B] Sorry if the title is not really precise.
I created a NPC that spawns a car on the [URL=http://steamcommunity.com/sharedfiles/filedetails/?id=771487490]Simfphys base[/URL], 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 [U]lua/entities/npc_car/init.lua[/U]):
[CODE]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[/CODE]
I tried to remove the previous car spawned by the player using [URL=http://wiki.garrysmod.com/page/Entity/Remove]Entity:Remove[/URL] 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.
[QUOTE=Invule;52755285]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.[/QUOTE]
And how can I remove the previous car ?
[QUOTE=Quozul;52755309]And how can I remove the previous car ?[/QUOTE]
When the entity is made, you're going to want to check if a value is not true;
[CODE]if not ply.SpawnedSimfphysVehicle then[/CODE]
and then set the variable on the player to true
[CODE]ply.SpawnedSimfphysVehicle = true[/CODE]
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:
[CODE]
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
[/CODE]
[QUOTE=DahDestroyer;52755320]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:
[CODE]
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
[/CODE][/QUOTE]
Where do I need to place this code ?
[QUOTE=Quozul;52755369]Where do I need to place this code ?[/QUOTE]
Wherever the entity is being spawned.
You would implement it like so:
[CODE]
function <whateverthehellitis>(ply)
if HasSpawnedSimfphysVehicle(ply, "sim_fphys_bak_taxi") then return end
--Rest of the functions code
end
[/CODE]
Assuming your function is correct (the one you posted - I don't have simfphys so can't test)
[CODE]
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
[/CODE]
[QUOTE=DahDestroyer;52755378]Wherever the entity is being spawned.
You would implement it like so:
[CODE]
function <whateverthehellitis>(ply)
if HasSpawnedSimfphysVehicle(ply, "sim_fphys_bak_taxi") then return end
--Rest of the functions code
end
[/CODE]
Assuming your function is correct (the one you posted - I don't have simfphys so can't test)
[CODE]
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
[/CODE][/QUOTE]
Is what I've done right ? Sorry if I'm retard
[CODE]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[/CODE]
Change ply to Caller inside the AcceptInput function.
The problem is I can't respawn a new car after removeing my previous one.
[QUOTE=DahDestroyer;52755420]Change ply to Caller inside the AcceptInput function.[/QUOTE]
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
[QUOTE=Quozul;52755450]The problem is I can't respawn a new car after removeing my previous one.
I edited my previous post.[/QUOTE]
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.
[CODE] 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[/CODE]
Here you set [I]"Caller.SpawnedSimfphysVehicle"[/I] 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 [I]"if Caller.SpawnedSimfphysVehicle == false then"[/I] check since you already checking if the player has a car.
[QUOTE=gmoddertr;52755508]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.
[CODE] 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[/CODE]
Here you set [I]"Caller.SpawnedSimfphysVehicle"[/I] 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 [I]"if Caller.SpawnedSimfphysVehicle == false then"[/I] check since you already checking if the player has a car.[/QUOTE]
So do I need to do something like this in the AcceptInput function ?
[CODE] 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[/CODE]
[CODE]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
[/CODE]
untested
[QUOTE=Blu-x92;52755609][CODE]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
[/CODE]
untested[/QUOTE]
Thanks this works !
[QUOTE=gmoddertr;52755508]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.[/QUOTE]
I probably explain myself badly
Sorry, you need to Log In to post a reply to this thread.