Jump to content

[Help] Vehicle Collision || Destroy


Sawos

Recommended Posts

Hello Community, i've been coding a very small Script which makes me a spawn a specific "Vehicle" with a command, It workd literally fine at first. Later on, i've tried to add this : 

setElementCollisionsEnabled(car, false)

to the car which i spawn, but the thing is once i spawn the vehicle, it goes directly up into the Air like there is none of Gravity if you know i mean, here is the code : 


allowedTeams = {  ["Staff"] = true,}

function spawner ( sourceplayer, commandname) 
if allowedTeams[getTeamName(getPlayerTeam( sourceplayer))]then 
local x, y, z = getElementPosition ( sourceplayer )
local location = getZoneName ( x, y, z )
 local car = createVehicle ( 445, x, y, z)
 warpPedIntoVehicle ( sourceplayer, car ) 
 setElementCollisionsEnabled(car, false)
 setVehiclePlateText( car, 'housing' )
 setVehicleHandling(car , "maxVelocity", 40) 
		setVehicleLocked ( car, true )
		setVehicleDamageProof(car, true)
		local vehicleName = getVehicleName (car)
outputChatBox ( "Your vehicle is being destroyed in few Seconds due to the Abuse", sourceplayer, 255, 255, 0 )

outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Spawned "..vehicleName.." At {" ..location.."}.")
setTimer ( function()
 destroyElement (car)
  
 end, 25000, 1 ) 
 else 
 setElementCollisionsEnabled(car, true)
 outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Tried the Housing command.")
 end
end
addCommandHandler ( "spawn", spawner ) 




function out ( sourceplayer )
if ( sourceplayer )then 
	
	setTimer( destroyElement, 50, 1, sourceplayer)


		end
end
addEventHandler ( "onPlayerVehicleExit", getRootElement(), out )  

One More thing, if you could help me with which the Destroying Vehicle thing cause every player who leaves his vehicle, it gets an instant Destroy as the function out, i just wanna make to the guy who spawned the vehicle Only. 

I would really appreciate your support, 

Thanks.

Edited by Saw^^
English failure
Link to post

First off, don't do setElementCollisionsEnabled on vehicles, not a happy ending.....  The good news is, there is a client side function that is setElementCollidableWith

Please carefully read the wiki, it clearly says if you use setElementCollisionsEnabled on a vehicle it will cause bugs.....  I know the temptation to grab code, but it is all useful information.

 

I have a small script I made ages ago (with help on forums as I was a noob) that limits players to having one vehicle, all you'll need to do is change the part that checks if they have a vehicle and make it destroy there vehicle when they exit! :)

function Elegy (hitElement) --Get source and pass it as hitElement
	local theVeh = getElementData( hitElement, "freecar" ) --Get the players data as it is set to there vehicle
	if ( theVeh ~= false ) then --If the player has the element data they have spawned a car
		destroyElement(theVeh) --So we destroy the car here
		setElementData(hitElement, "freecar", false) --Now we want to set the data to false, this is important!!!
	end
   	local x, y, z = getElementPosition (hitElement) --Get the players position
   	local rotX, rotY, rotZ = getElementRotation (hitElement) --Get their rotation
   	veh = createVehicle (562, x, y+5, z, rotX, rotY, 270) --Create a vehicle
   	warpPedIntoVehicle (hitElement, veh) --Warp player into vehicle
   	setElementData(hitElement, "freecar", veh) --Set the players data to the vehicle you just created, we can use the data to destroy the vehicle
end

addCommandHandler("spawnElegy", Elegy)

Hope this helps, use "/debugscript 3" when you have issues on your script, it help A LOT. :)

Link to post

I'm using the debugscript command everytime i log in. 

That's why i needed some help cause the debug was showing no Warnings/errors at all. 

Will try out what u just replied, thanks

Since it's server side and  setElementCollidableWith works with Client Only, do u have any idea how may i code it to client side so i can trigger it to server side, 

In other words, Shall i use the GetElementData on the  funtcion of  setElementCollidableWith (client side) !! 

Link to post
16 minutes ago, Saw^^ said:

I'm using the debugscript command everytime i log in. 

That's why i needed some help cause the debug was showing no Warnings/errors at all. 

Will try out what u just replied, thanks

Forgot to mention, you can use the element data you set when a vehicle spawns for the player client and server side, they share it, so a little element data is good, LOTS of element data being passed is very bad, as it's CPU intensive...  Just be wary of using it since you are a fairly new scripter, also you should check out IIYAMA's tutorial on debugging, it isn't just /debugscript 3, he walks you through manual debugging :)

Manual debugging is super useful, the way the debugscript works is it just runs through some simple checks, checking if you have certain information at certain points, this is making it limited, so it sometimes will show nothing, so just keep in mind that manual debugging is there :D

 

Edited by kieran
Link to post
function colloff ()
local theVeh = getElementData( sourceplayer, "freecar" )
setElementCollidableWith(theVeh, sourceplayer, false)
end
addEvent( "test", true )
addEventHandler( "test", localPlayer, colloff ) 

This should work fine ? when i trigger it to server side ?

bad argument @'setElementCollidableWith' [expected element at argument1, got boolean]

Link to post
15 minutes ago, Saw^^ said:

function colloff ()
local theVeh = getElementData( sourceplayer, "freecar" )
setElementCollidableWith(theVeh, sourceplayer, false)
end
addEvent( "test", true )
addEventHandler( "test", localPlayer, colloff ) 

This should work fine ? when i trigger it to server side ?

Well, first of all you have localPlayer where root should be, read addEventHandler on the wiki :)

You are trying to pass "sourceplayer" when it's not included in your function, to sort this, since this script is client side, use localPlayer.

Also you are just going to set the vehicle so it can't collide with one player (I think) I have not used setElementCollidableWith yet as I have had no use for it....  But I will try fix your code a little :D

function colloff ()
	local theVeh = getElementData( localPlayer, "freecar" )
  	--local v = getPedOccupiedVehicle(localPlayer) --If you still have no luck try this
  	for index,vehicle in ipairs(getElementsByType("vehicle")) do --LOOP through all Vehicles, if you want it to be players change it to player
		setElementCollidableWith(vehicle, theVeh, false) -- Set the Collison off with the Other vehicles, (the vehicles, your vehicle, state)
  end
end
addEvent( "test", true )
addEventHandler( "test", root, colloff ) 

Could work, could not work, if you really want I can test it and get back to you :)

Edited by kieran
Link to post

Disabling collision to a vehicle will prevent anything colliding with it, even the world. What you'd want to do is set what it can collide with, other vehicles, players, obects etc.. Using setElementCollidableWith()

Link to post
local vehicle = getPedOccuipiedVehicle(localPlayer)
for i,v in pairs(getElementsByType('vehicle')) do
  setElementCollidableWith(vehicle,v,false)
end

This code is an example on client side, it will disable collision between vehicles, this works as like a ghostmode type script.

Link to post
setElementCollisionsEnabled(car, false)
to the car which i spawn, but the thing is once i spawn the vehicle, it goes directly up into the Air like there is none of Gravity if you know i mean, here is the code : 

📋

allowedTeams = {  ["Staff"] = true,}

function spawner ( sourceplayer, commandname) 
if allowedTeams[getTeamName(getPlayerTeam( sourceplayer))]then 
local x, y, z = getElementPosition ( sourceplayer )
local location = getZoneName ( x, y, z )
 local car = createVehicle ( 445, x, y, z)
 warpPedIntoVehicle ( sourceplayer, car ) 
 setElementCollisionsEnabled(car, false)
 setVehiclePlateText( car, 'housing' )
 setVehicleHandling(car , "maxVelocity", 40) 
		setVehicleLocked ( car, true )
		setVehicleDamageProof(car, true)
		local vehicleName = getVehicleName (car)
outputChatBox ( "Your vehicle is being destroyed in few Seconds due to the Abuse", sourceplayer, 255, 255, 0 )

outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Spawned "..vehicleName.." At {" ..location.."}.")
setTimer ( function()
 destroyElement (car)
  
 end, 25000, 1 ) 
 else 
 setElementCollisionsEnabled(car, true)
 outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Tried the Housing command.")
 end
end
addCommandHandler ( "spawn", spawner ) 




function out ( vehicle )
if vehicle then else return end
if getElementData(vehicle,"housing") then else return end
    destroyElement(vehicle)
	
	


		end
addEventHandler ( "onPlayerVehicleExit", getRootElement(), out ) 

I wrote it on phone so i couldnt delete spaces

Link to post

Personally I would use onClientPlayerVehicleExit and not onPlayerVehicleExit as you are using client side code, this would probably make it easier and more readable....  URBAN is along the right lines, but he was on phone, so it's expected he had some problems....  I will try restructure his code in a way the server can read it, and fix any problems I come across...

Server side

allowedTeams = {  ["Staff"] = true,}

function spawner ( sourceplayer, commandname ) 
	if allowedTeams[getTeamName(getPlayerTeam( sourceplayer))]then 
		local x, y, z = getElementPosition ( sourceplayer )
		local location = getZoneName ( x, y, z )
 		local car = createVehicle ( 445, x, y, z)
    	local theVeh = setElementData( sourceplayer, "housing", car ) --We set the players element data and pass the spawned car as a value, saving that car to the player until there data is set to false or they reconnect...
 		warpPedIntoVehicle ( sourceplayer, car ) 
 		setVehiclePlateText( car, 'housing' )
 		setVehicleHandling(car , "maxVelocity", 40) 
		setVehicleLocked ( car, true )
		setVehicleDamageProof(car, true)
		local vehicleName = getVehicleName (car)
		outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Spawned "..vehicleName.." At {" ..location.."}.")
		--[[setTimer ( function()
 			destroyElement (car)
  		end, 25000, 1 )]] --Just commented it out as it's spawning here, NOT destroying 
 	else 
 		outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Tried the Housing command.")
 	end
end

addCommandHandler ( "spawn", spawner ) 




function out ( player ) --He was returning the PLAYER... ALWAYS check the source element on the wiki.
if player then
	vehicle = getElementData(vehicle,"housing") --This returns the vehicle if the player has the data.
	if isElement(vehicle) then --Next you should double check to make sure the element is there.
        destroyElement(vehicle) --If so you destroy it
      	local setElementData( player, "housing", false ) --We also want to reset their data (set it false) if it's destroyed so they can spawn another one.
    else 
        outputDebugString ( "Housing Admin: [" ..getPlayerName(player).."] Has left their vehicle and it was NOT destroyed.") --send a message
    end
end
  
addEventHandler ( "onPlayerVehicleExit", getRootElement(), out ) 

Client side

function colloff ()
	local theVeh = getElementData( localPlayer, "housing" )
  	for index,vehicle in ipairs(getElementsByType("vehicle")) do --LOOP through all Vehicles, if you want it to be players change it to player
		setElementCollidableWith(vehicle, theVeh, false) -- Set the Collison off with the Other vehicles, (the vehicles, your vehicle, state)
  end
end
addEvent( "HousingCollisionless", true ) --renamed in case there was any conflicts, remember, handler are triggered across all scripts if they are true, not just the one they are in.
addEventHandler( "HousingCollisionless", root, colloff ) 

If it doesn't work this time I'm just going to test and debug myself...  But hopefully it will be a little better. :)

Edited by kieran
Link to post

Loading script failed: housingspawn\spawner.lua:34: ')' expected near ',' 

do i have to add the vehicle identify name ? 

Removed The "local" from SetElementData at the function Out and Now it shows me : attempt to concatenate a boolean value

Link to post
    allowedTeams = {  ["Staff"] = true,}
    function spawner ( sourceplayer, commandname ) 
    	if allowedTeams[getTeamName(getPlayerTeam( sourceplayer))]then 
    		local x, y, z = getElementPosition ( sourceplayer )
    		local location = getZoneName ( x, y, z )
     		local car = createVehicle ( 445, x, y, z)
        	setElementData( source, "housing", car ) --We set the players element data and pass the spawned car as a value, saving that car to the player until there data is set to false or they reconnect...
     		warpPedIntoVehicle ( sourceplayer, car ) 
     		setVehiclePlateText( car, 'housing' )
     		setVehicleHandling(car , "maxVelocity", 40) 
    		setVehicleLocked ( car, true )
    		setVehicleDamageProof(car, true)
    		local vehicleName = getVehicleName (car)
    		outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Spawned "..vehicleName.." At {" ..location.."}.")
    		--[[setTimer ( function()
     			destroyElement (car)
      		end, 25000, 1 )]] --Just commented it out as it's spawning here, NOT destroying 
     	else 
     		outputDebugString ( "Housing Admin: [" ..getPlayerName(sourceplayer).."] Has Tried the Housing command.")
     	end
    end
    addCommandHandler ( "spawn", spawner ) 
    function out ( player ) --He was returning the PLAYER... ALWAYS check the source element on the wiki.
    if player then
    	vehicle = getElementData(vehicle,"housing") --This returns the vehicle if the player has the data.
    	if isElement(vehicle) then --Next you should double check to make sure the element is there.
            destroyElement(vehicle) --If so you destroy it
          	local setElementData( player, "housing", false ) --We also want to reset their data (set it false) if it's destroyed so they can spawn another one.
        else 
            outputDebugString ( "Housing Admin: [" ..getPlayerName(player).."] Has left their vehicle and it was NOT destroyed.") --send a message
        end
    end
      
    addEventHandler ( "onPlayerVehicleExit", getRootElement(), out ) 

My bad, copy and pasted getElementData as it was easy to keep the "housing" part, I am lazy haha....  Should work now.

Basically I set setElementData as a variable, you shouldn't do that, you should only do it with getElementData :)

Edited by kieran
Link to post

Check through it dude, the problem was obvious :) just use /debugscript 3, look at the line number, script, and go to that line....  I forgot I changed vehicle to player on your vehicle exit command, the source of the function (the player) now is "player" and not "vehicle" since we are checking and destroying the vehicle....  So we want to replace line 25 with the following.

vehicle = getElementData(player,"housing")

Notice how I changed the first argument in getElementData from vehicle (which wouldn't work as that doesn't exist) to player (the player we are retrieving the data from)

Hopefully it's sorted now, it's just a case of reading the errors it says, then thinking of all the possible things that are wrong where it says they are

Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...