Jump to content

getElementID isn't working..


kevin433

Recommended Posts

Hi,

I'm trying to prevent the player from entering a vehicle if he's not the owner. The problem is that "getElementID" doesn't get the ID from the Vehicle. I'm using the function called "onClientVehicleStartEnter", I've read at the Wiki that the parameters are "thePlayer, seat, door" and that the source is the Vehicle the player enters. So I wonder why it's not getting the Vehicle ID.

function checkvehicleowner(player)
local vre = executeSQLSelect("Vehicles", "Owner", "CarID = '" .. getElementID(source) .."'")
outputChatBox(vre[1].Owner)
if(vre[1].Owner == getPlayerName(player)) then
setVehicleLocked(source, false)
outputChatBox(vre[1].Owner)
else
outputChatBox("Du bist nicht der besitzer diese fahrzeugs!", getLocalPlayer())
end
end
addEventHandler("onClientVehicleStartEnter", getRootElement(), checkvehicleowner)

I see nothing wrong with this code, it's giving me no errors in the MTA:SE nor in the Console when I start to enter the vehicle. I also tested out how far the code proceeds with textmessages between the lines. It only goes to the line with "local vre = executeSQLSelect("Vehicles", "Owner", "CarID = '" .. getElementID(source) .."'")".

Link to comment

The getElementID documentation states the problem with how you're using the function:

This function gets the ID of an element. This is the "id" attribute of the element and is a string, NOT a number like a model ID, weapons ID or similar.

This means it also doesn't return a vehicle ID. For this, you should use getElementModel.

So what's getElementID for? It's for getting the ID defined in maps. With these IDs, you can get individual objects so you can move them etc. But as you want the vehicle ID, you need to use getElementModel.

I recommend you to read the wiki a bit more thoroughly. ;)

Link to comment

well SQL functions obviously are server-side, since database is server-side :D

you can also use setElementData (which is synchronized) to set vehicle owner when vehicle is spawned or bought by someone:

setElementData(theVehicle, "vehicleowner", ownerName)

and then you can access it client-side:

if getElementData(theVehicle, "vehicleowner") == getPlayerName(player) then

Link to comment

Thank you xbost! Now it works! But now I'm having another problem. If I'm not the owner it doesn't cancel the event :(

function checkvehicleowner(player)
if(getElementData(source, "Owner") == getPlayerName(player)) then
setVehicleLocked(source, false)
outputChatBox(getElementData(source, "Owner"))
else
outputChatBox("Du bist nicht der besitzer diese fahrzeugs!", player)
cancelEvent()
end
end
addEventHandler("onClientVehicleStartEnter", getRootElement(), checkvehicleowner)

Link to comment

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...