Cars summoned with LUA do nothing, and can be walked through.
9 replies, posted
What am I doing wrong?
[CODE]
local car = ents.Create("prop_vehicle_jeep_old")
car:SetModel("models/buggy.mdl")
car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
car:SetPos( ply:GetPos() + Vector(10,0,20) )
car:SetOwner( ply )
car:Spawn()
[/CODE]
Gotta initialize its physics entity, its been a long time since I've done that so someone will likely pop up with the code
Is the car model even on server? Because walking trhough it means there's no a phys model for that car = no model in server
[QUOTE=Lavacoal123;52316982]What am I doing wrong?
[CODE]
local car = ents.Create("prop_vehicle_jeep_old")
car:SetModel("models/buggy.mdl")
car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
car:SetPos( ply:GetPos() + Vector(10,0,20) )
car:SetOwner( ply )
car:Spawn()
[/CODE][/QUOTE]
[img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetOwner]Entity:SetOwner[/url] disables physics interaction(collisions) with the player, it doesn't give the player ownership of the car.
[QUOTE=bilbasio;52317532][img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Entity/SetOwner]Entity:SetOwner[/url] disables physics interaction(collisions) with the player, it doesn't give the player ownership of the car.[/QUOTE]
How can you give the player ownership of the car? I have a hook on my gamemode (from [URL="https://facepunch.com/showthread.php?t=1566466"]this thread[/URL]) that disallows people who are not owners of the car from going inside of it...
[CODE]
hook.Add( "CanPlayerEnterVehicle", "Debug.CanPlayerEnterVehicle.Disallow", function( _Player, _Vehicle)
if not _Vehicle:IsValidVehicle() then return end
local __VehOwner = _Vehicle:GetOwner()
if __VehOwner != _Player then
_Player:PrintMessage( HUD_PRINTTALK, "This isn't your car!" )
return false
end
end )
[/CODE]
Edit: I could use SetPhysicsAttacker() and GetPhysicsAttacker() but how would I modify the hook to work like that? I didn't make it, props to kpjVideo, hook works like a charm
Edit x2: Maybe like so?
[CODE]
hook.Add( "CanPlayerEnterVehicle", "Debug.CanPlayerEnterVehicle.Disallow", function( _Player, _Vehicle)
if not _Vehicle:IsValidVehicle() then return end
local __VehAttacker = _Vehicle:GetPhysicsAttacker()
if __VehAttacker != _Player then
_Player:PrintMessage( HUD_PRINTTALK, "This isn't your car!" )
return false
end
end )
[/CODE]
Use Get/SetCreator.
[QUOTE=NeatNit;52318943]Use Get/SetCreator.[/QUOTE]
I want the owner to get kill credit
Untested but this should work:
[code]local car = ents.Create("prop_vehicle_jeep_old")
car:SetModel("models/buggy.mdl")
car:SetKeyValue("vehiclescript","scripts/vehicles/jeep_test.txt")
car:SetPos( ply:GetPos() + Vector(10,0,20) )
car.Owner = ply --Set the player as the car owner
car:Spawn()
hook.Add("CanPlayerEnterVehicle", "AllowVehicleEntry", function(ply, veh)
if not IsValid(veh) or not IsValid(ply) then return end
if not veh.Owner then return true end --If there's no owner let them enter the vehicle
if not veh.Owner == ply then return false end --If the player is not the owner then dont let them enter
return true --If it gets past the three conditions above let them enter
end)
hook.Add("PlayerDeath", "CheckTheKiller", function(victim, inflitor, attacker)
if attacker:GetClass() == "prop_vehicle_jeep" then --If the attacker is a vehicle
if attacker.Owner then --If it has an owner
attacker.Owner:AddFrags(1) --Add a frag to the owner of the vehicle
end
end
end)[/code]
[QUOTE=bilbasio;52319271]Untested but this should work:
:snip:[/QUOTE]
I'd use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]GM:EntityTakeDamage[/url] instead, this would make it apply in all scenarios, for example, NPC kills or addons that do stuff on PvP damage even if it doesn't kill. Cover all bases.
However, I recommend an arguably better approach: use SetCreator like I said, but whenever a player enters a car, use SetOwner. When the player exits the car, unset the owner. That would behave like you want, I think.
Edit: it would also make it easier to later on allow someone else besides the owner to drive the car.
[QUOTE=NeatNit;52319555]I'd use [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/GM/EntityTakeDamage]GM:EntityTakeDamage[/url] instead, this would make it apply in all scenarios, for example, NPC kills or addons that do stuff on PvP damage even if it doesn't kill. Cover all bases.
However, I recommend an arguably better approach: use SetCreator like I said, but whenever a player enters a car, use SetOwner. When the player exits the car, unset the owner. That would behave like you want, I think.
[/QUOTE]
Sounds like a decent idea. I just realised the point system doesn't require kill credit.
[QUOTE=NeatNit;52319555]
Edit: it would also make it easier to later on allow someone else besides the owner to drive the car.[/QUOTE]
Already thought about that. I made an exception for one user: me. This is just for testing purpouses because I have to spawn a functional car.
[editline]6th June 2017[/editline]
[QUOTE=bilbasio;52319271]Untested but this should work:
[code]
if not veh.Owner then return true end --If there's no owner let them enter the vehicle
[/code]
[/QUOTE]
Getting rid of this line because of how the gamemode works. I only want them to enter a vehicle that is EXPLICITLY theirs, one that is spawned when they first spawn. They can only use THIS car.
[QUOTE=bilbasio;52319271]
[code]
attacker.Owner:AddFrags(1) --Add a frag to the owner of the vehicle
end)[/code][/QUOTE]
What's this doing? I allready have a point system if that's what you're going for. It uses networked integers but I might just do this. It looks more efficient.
[B]Edit:[/B]
Thanks for all the help guys. Have a nice day!
Sorry, you need to Log In to post a reply to this thread.