Jump to content

Problem with dxDrawTextOnElement


ivan8065

Recommended Posts

Hello, I was trying to make script that would show the name of bus stop in front of bus.

 

I started by trying this example from wiki:

local randomPed = createPed(285, 2476.91406, -1665.31799, 13.32435)

addEventHandler("onClientRender", getRootElement(), 
function()
	dxDrawTextOnElement(randomPed, "SWATTEAM Officer", 1, 20, 0, 0, 255, 255, 1, "pricedown")
end)

but it throws me this error: client.lua:5 attempt to call global 'dxDrawTextOnElement' (a nil value)

 

Was the function removed from MTA or what can be problem please? Thank you for reply.

Link to comment
  • Moderators

No, it's just that Useful functions are functions made by the community and that are not built-in into MTA.

image.thumb.png.34b5de14fa702112ca0a481f1f525a54.png

To use them, you have to copy the function code first. And then you can call it (the function source code is lower on the wiki page: https://wiki.multitheftauto.com/wiki/DxDrawTextOnElement

local randomPed = createPed(285, 2476.91406, -1665.31799, 13.32435)

addEventHandler("onClientRender", getRootElement(), 
function()
	dxDrawTextOnElement(randomPed, "SWATTEAM Officer", 1, 20, 0, 0, 255, 255, 1, "pricedown")
end)

-- Function source copied from wiki https://wiki.multitheftauto.com/wiki/DxDrawTextOnElement
function dxDrawTextOnElement(TheElement,text,height,distance,R,G,B,alpha,size,font,...)
	local x, y, z = getElementPosition(TheElement)
	local x2, y2, z2 = getCameraMatrix()
	local distance = distance or 20
	local height = height or 1

	if (isLineOfSightClear(x, y, z+2, x2, y2, z2, ...)) then
		local sx, sy = getScreenFromWorldPosition(x, y, z+height)
		if(sx) and (sy) then
			local distanceBetweenPoints = getDistanceBetweenPoints3D(x, y, z, x2, y2, z2)
			if(distanceBetweenPoints < distance) then
				dxDrawText(text, sx+2, sy+2, sx, sy, tocolor(R or 255, G or 255, B or 255, alpha or 255), (size or 1)-(distanceBetweenPoints / distance), font or "arial", "center", "center")
			end
		end
	end
end

^ Like this, your script will work

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