Jump to content

Get objects near player


rapgod1

Recommended Posts

Hello everyone. I tried to do a system, when player from one clan do anything, to near the player checked objects. 3 months ago it's worked, but now not :/

function checkClick(button, state)
    if (button == "-") and state and tempOb[localPlayer] --[[and not isGui]] then
        local dist = 50
        local x,y,z = getElementPosition(getLocalPlayer())
        local xr,yr,zr = getElementRotation(getLocalPlayer())
        local px, py, pz = getElementPosition(getLocalPlayer())
        local prot = getCameraMatrix(getLocalPlayer())
        local offsetRot = math.rad(prot+90)
        local vx = px + dist * math.cos(offsetRot)
        local vy = py + -(dist) * math.sin(offsetRot)
        local vz = pz
        local vrot = prot+180
        local hit,x,y,z,elementHit = processLineOfSight(x,y,z,vx,vy,vz)
        local x1,y1,z1 = getElementPosition(localPlayer)
        local x2,y2,z2 = getElementPosition(tempOb[localPlayer])
        if isElementWithinMarker(localPlayer, area51) then return end
        if isElementWithinMarker(localPlayer, zaerolv) then return end
        if isElementWithinMarker(localPlayer, sfarmy) then return end
        if getDistanceBetweenPoints3D(x1,y1,z1,x2,y2,z2) > 5 then
            exports.dayz:startRollMessage2("Inventory", "Подойдите ближе к объекту!", 255, 255, 0)
            return
        end
        if elementHit and isElement(elementHit) and getElementType(elementHit) == "object" then -- HERE 
            local objectCreator = getElementData(elementHit,"bc.creator")
            local objectGroup = getElementData(elementHit,"bc.gang")
            if objectCreator then
                if getElementData(localPlayer,"playerlogin") == objectCreator or getElementData(localPlayer,"gang") == objectGroup then
                    local x, y, z = getElementPosition(tempOb[localPlayer])
                    local rx, ry, rz = getElementRotation(tempOb[localPlayer])
                    local model = getElementModel(tempOb[localPlayer])
                    removeEventHandler("onClientRender", root, updatePos)
                    destroyElement(tempOb[localPlayer])
                    tempOb[localPlayer] = nil
                    triggerServerEvent("basecreator:newObject", localPlayer, model, x, y, z, rx, ry, rz, health )
                    active = false
                    removeEventHandler("onClientKey", root, checkClick)
                    removeEventHandler("onClientKey",root,setRotationOfObject)
                    removeEventHandler("onClientKey", root, handleObDelete)
                    takeItemsFromPlayer(model)
                    guiSetVisible(baseCPanel.window[1], true)
                    showCursor(not isCursorShowing())
                else
                    exports.dayz:startRollMessage2("Inventory", "Здесь нельзя строить!", 255, 255, 0)
                end
            end
        else       
            local x, y, z = getElementPosition(tempOb[localPlayer])
            local rx, ry, rz = getElementRotation(tempOb[localPlayer])
            local model = getElementModel(tempOb[localPlayer])
            removeEventHandler("onClientRender", root, updatePos)
            destroyElement(tempOb[localPlayer])
            tempOb[localPlayer] = nil
            triggerServerEvent("basecreator:newObject", localPlayer, model, x, y, z, rx, ry, rz, health )
            active = false
            removeEventHandler("onClientKey", root, checkClick)
            removeEventHandler("onClientKey",root,setRotationOfObject)
            removeEventHandler("onClientKey", root, handleObDelete)
            takeItemsFromPlayer(model)
            guiSetVisible(baseCPanel.window[1], true)
            showCursor(not isCursorShowing())
        end
    end
end

What's wrong?

Link to comment

I'm not really sure what you want to know, but to answer things I understood:

Function processLineOfSight can get you only one object (hitElement), you should store objects to table and check nearby objects using for function.

Or you can use getElementsByType("object", resourceRoot) to get all objects created by the same resource, then you can use already mentioned for function.

 

Example (untested):

function getNearbyObjects(thePlayer)
	local nearbyObjects = { }
	local px,py,pz = getElementPosition(thePlayer)
	for k,v in pairs(getElementsByType("object", resourceRoot))do
		local vx,vy,vz = getElementPosition(thePlayer)
		if(getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz) < 5)then
			table.insert(nearbyObjects, v)
		end
	end
	return nearbyObjects
end

This function will output table of all objects within same resource that have distance to player less than 5. You can implement it and edit for your needs.

Edited by Rataj
  • Like 1
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...