Jump to content

[Help] Error when i create one text and ID


Recommended Posts

Well, I explain I am trying to make a system in environments on my server but I have 2 problems and they are the following: Not detecting the ID and the text, the ID is inserted in the database but the text is not. I hope you can help me, thanks.

 

 

CODE : 
 

local elements = { }

local function loadText( id, text, x, y, z, interior, dimension  )
	local element = createElement( "3dtext" )
	setElementPosition( element, x, y, z )
	setElementInterior( element, interior )
	setElementDimension( element, dimension )
	setElementData( element, "text", tostring( text ) )
	
	elements[ id ] = element
end

addEventHandler( "onResourceStart", resourceRoot,
	function( )

		local result = dbPoll(dbQuery(handler, "SELECT * FROM 3dtext ORDER BY textID ASC" ),-1)
		if result then
			for key, data in ipairs( result ) do
				loadText( data.textID, data.text, data.x, data.y, data.z, data.interior, data.dimension )
			end
		end
	end
)






local function createtext(player,cmd,textID,...)
	if GetElementData(player,"AdminLvl", 5) then
			local text = table.concat( { ... }, " " )
			local x, y, z = getElementPosition( player )
				local insertid = dbExec ( handler, "INSERT INTO `3dtext`(`text`, `x`, `y`, `z`, `interior`, `dimension`) VALUES (?,?,?,?,?,?)", text, x, y, z, getElementInterior( player ),getElementDimension( player ) )
				if insertid then
					loadText( insertid, text, x, y, z, getElementInterior( player ), getElementDimension( player ) )
					outputChatBox( "Text created. (ID " .. insertid .. ")", player, 0, 255, 0 )
				else
					outputDebugString( e )
					outputChatBox( "Error MYSQL.", player, 255, 0, 0 )
				end
			else
				outputChatBox( "Command: /" .. cmd .. " [text]", player, 255, 255, 255 )
			end
		end
addCommandHandler("createtext",createtext)

(Sorry for my bad english)

In the debugscript show´s me this error  : In the line 37 : attempt to concatenate local 'insertid' (a boolean value)

And this is how it create  https://imgur.com/2Je5B9T

Link to comment
  • Moderators

https://wiki.multitheftauto.com/wiki/DbExec

Quote

Returns

Returns true unless the connection is incorrect, in which case it returns false.

 

You need to use dbQuery with callback.

-- Something like this

local function createtext(player,cmd,textID,...)
	if GetElementData(player,"AdminLvl", 5) then
		local text = table.concat( { ... }, " " )
		local x, y, z = getElementPosition( player )
			dbQuery(function(qh) -- callback function
				local result, inserted_rows, lastinsertedid = dbPoll(qh, 0)
				if lastinsertedid then
					loadText( lastinsertedid, text, x, y, z, getElementInterior( player ), getElementDimension( player ) )
					outputChatBox( "Text created. (ID " .. lastinsertedid .. ")", player, 0, 255, 0 )
				else
					outputDebugString( e )
					outputChatBox( "Error MYSQL.", player, 255, 0, 0 )
				end
			end, handler, "INSERT INTO `3dtext`(`text`, `x`, `y`, `z`, `interior`, `dimension`) VALUES (?,?,?,?,?,?)", text, x, y, z, getElementInterior( player ),getElementDimension( player ) )
		else
			outputChatBox( "Command: /" .. cmd .. " [text]", player, 255, 255, 255 )
		end
	end
addCommandHandler("createtext",createtext)

 

Edited by stPatrick
  • Thanks 1
Link to comment
  • Scripting Moderators
6 minutes ago, Homer Simpson said:

It works now but i need to put : /createtext [Something there] [text] and then it works but the ID all the time it´s 1 and in the DB shows the good ID but IN GAME not.


 

Perhaps you use maybe wrapper for element data?

if GetElementData(player,"AdminLvl", 5) then

Should be:

if getElementData(player,"AdminLvl", 5) then

Lua is case sensitive.

  • Thanks 1
Link to comment
  • Moderators
5 minutes ago, Homer Simpson said:

It works now but i need to put : /createtext [Something there] [text] and then it works but the ID all the time it´s 1 and in the DB shows the good ID but IN GAME not.

 

Why need textID?

Use Auto Increment, it's the easiest and best way.

 

Helps

https://www.youtube.com/watch?v=pzGMAkqRtAI

https://stackoverflow.com/questions/5665571/auto-increment-in-phpmyadmin

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