Hello, I want to check if object is near a player in a specifed radius but it is not working, here is what i have:
Citizen.CreateThread(function()
while true do
Citizen.Wait(200)
local pedCoords = GetEntityCoords(GetPlayerPed(-1))
if GetClosestObjectOfType(pedCoords.x, pedCoords.y, pedCoords.z, 5.0, GetHashKey("prop_plant_int_02a"), true, true, true) then
print("object near!")
else
print("object not in radius")
end
end
end)
So 5.0 after pedCoords.z in GetClosestObjectOfType stands for radius right? so it should print “object near” when in radius, but the problem is if i move away it is still printing “object near” even if not in radius, how to make it work properly?
Citizen.CreateThread(function()
while true do
Citizen.Wait(5)
local pedCoords = GetEntityCoords(PlayerPedId())
local objectId = GetClosestObjectOfType(pedCoords, 5.0, GetHashKey("prop_plant_int_02a"), false)
if DoesEntityExist(objectId) then
print("Object Is Near: " .. objectId)
end
end
end)
Awesome! works, but i have another question. I added DeleteObject(objectId) after that print and it does delete the object but only for the player who spawned it, so is there a way so others could delete it even if they not have spawned it? i also tried to used SetEntityCoords to teleport it under map but nothing seems to work, if it can be printed (objectId) in console it could also delete it right? it is not networking right or what? or maybe any idea how to make it only accessible for player who spawned it? Thank you anyway
You spawn the object for everyone? and you want to delete it for everyone? Trigger an server event that triggers a client event and deletes the objectId.
Im not sure if im doing it right because it is not working, i have this client side:
Citizen.CreateThread(function()
while true do
Citizen.Wait(1500)
local pedCoords = GetEntityCoords(PlayerPedId())
local objectId = GetClosestObjectOfType(pedCoords, 2.0, GetHashKey("prop_plant_int_02a"), false)
if DoesEntityExist(objectId) then
print("Object Is Near: " .. objectId)
TriggerServerEvent("test:forcedelete") --triggering server event as you said
end
end
end)
RegisterNetEvent('deleteobject:allow')
AddEventHandler('deleteobject:allow', function()
local pedCoords = GetEntityCoords(PlayerPedId())
local objectId = GetClosestObjectOfType(pedCoords, 2.0, GetHashKey("prop_plant_int_02a"), false)
DeleteObject(objectId)
end)
Should the object be created diffrent way? how to network it to work? Everybody is seeing the object as i have noticed so it should network and be able to delete for everyone not only for the one who spawned right?
Ok, if it works like I think. You should only need to use the host to create and delete entity, you can get the host id from the server with this, then just Trigger that client to create or delete
send the result of OBJ_TO_NET as an event, then on the client event handler for it just NETWORK_HAS_CONTROL_OF_NETWORK_ID, followed by NET_TO_OBJ and a delete.
So that doesn’t work as I thought ^^. But, if a player A create an object and share the network id to other players, then the player A disconnect, so player B take control of the net object, Can player B remove it with the same network id?
Citizen.CreateThread(function()
while true do
Citizen.Wait(5)
local pedCoords = GetEntityCoords(PlayerPedId())
local objectId = GetClosestObjectOfType(pedCoords, 2.0, GetHashKey("prop_plant_int_02a"), false)
if DoesEntityExist(objectId) and IsControlJustPressed(0, 38) then
print("Object Is Near: " .. objectId)
TriggerServerEvent("test:forcedelete")
end
end
end)
RegisterNetEvent('deleteobject:allow')
AddEventHandler('deleteobject:allow', function()
local pedCoords = GetEntityCoords(PlayerPedId())
local objectId = GetClosestObjectOfType(pedCoords, 2.0, GetHashKey("prop_plant_int_02a"), false)
local objectnet = ObjToNet(objectId)
if NetworkHasControlOfNetworkId(objectnet) then
DeleteObject(NetToObj(objectnet))
end
end)
---
RegisterNetEvent('object:createtestobject')
AddEventHandler('object:createtestobject', function()
local pedCoords = GetEntityCoords(PlayerPedId())
local testObject = CreateObject(GetHashKey("prop_plant_int_02a"), pedCoords.x, pedCoords.y, pedCoords.z, 1, 1, 1)
end)
And it works, but still only player who spawned the object, what im doing wrong?
As I said, you need to send the net id over the network.
So, in your loop, when you get the closest object, send to your server the net id TriggerServerEvent("test:forcedelete", ObjToNet(objectId)). Then in your server event, share the id to all players:
To finish, in the client event, just need to delete:
RegisterNetEvent('deleteobject:allow')
AddEventHandler('deleteobject:allow', function(netId)
if NetworkHasControlOfNetworkId(netId) then
DeleteObject(NetToObj(netId))
end
end)
Sorry for the inconvenience, I wanted to ask you how I can make a script recognize any object in front of it without having to identify it properly, I am trying that the “Forklift” script can not only pick up cars but also objects.