Save Entity Network Variables to Database..

Hi, I’ve recently started Lua and I’m having some issues with the database side of things. First off, excuse if the code is messy or wrong, I’ve only been doing lua for around 2 days and basically I’m trying to create a database that will save network variables from the relevant entity.

The problem that I’m having atm is whenever I run the command that runs the function, it returns this error in console:



  [ERROR] gamemodes/mygamemode/gamemode/database.lua:26: attempt to concatenate global 'doorOwnable' (a boolean value)
  1. SaveEntity - gamemodes/mygamemode/gamemode/database.lua:26
   2. unknown - gamemodes/mygamemode/gamemode/door.lua:7
    3. unknown - lua/includes/modules/concommand.lua:54


Here is the function that is executed when I enter the command (while looking at the entity)



function SaveEntity( ply )
	local ent = ply:GetEyeTrace().Entity
	doorID = ent:EntIndex()
	doorKeys = ent:GetNWInt("Keys")
	doorPrice = ent:GetNWInt("doorPrice")
	doorOwner = ent:GetNWString("Owner")
	doorNotice = ent:GetNWString("doorNotice")
	doorIsOwned = ent:GetNWBool("isOwned")
	doorPolice = ent:GetNWBool("policeDoor")
	doorOwnable = ent:GetNWBool("Ownable")

	sql.Query("UPDATE entity_info SET doorKeys = "..doorKeys..", doorPrice = "..doorPrice..", doorOwner = '"..doorOwner.."', doorNotice = '"..doorNotice.."', doorIsOwned = "..doorIsOwned..", doorPolice = "..doorPolice..", doorOwnable = "..doorOwnable.." WHERE doorID = "..doorID.."")
	ply:ChatPrint("Entity Saved")
end


And here is the table for it:



function OnTablesExist()
	if sql.TableExists("player_info") && sql.TableExists("entity_info") then
		Msg("Tables already exist")
	else
		if (!sql.TableExists("player_info")) then
			query = "CREATE TABLE player_info ( unique_id varchar(255), money int )"
			result = sql.Query(query)
			if (sql.TableExists("player_info")) then
				Msg("Success! player_info created 
")
			else
				Msg("Something went wrong 
")
				Msg( sql.LastError( result ) .. "
" )
			end
		end
		if (!sql.TableExists("entity_info")) then
			query = "CREATE TABLE entity_info ( doorID int, doorKeys int, doorPrice int, doorOwner text, doorNotice text, doorIsOwned boolean, doorPolice boolean, doorOwnable boolean )"
			result = sql.Query(query)
			if (sql.TableExists("entity_info")) then
				Msg("Success! entity_info created 
")
			else
				Msg("Something went wrong 
")
				Msg( sql.LastError( result ) .. "
" )
			end
		end
	end
end


If anyone knows why it’s returning this error, I’d be very grateful! Thanks.

Firstly, make your variables local, not global. Secondly, you can’t concatenate a boolean value, you should use tostring() on it.

Ah brilliant, I’ve localized my variables but I’m too sure where I’d use “tostring” or how; any chance of an example? :slight_smile:

[lua]
tostring( MyVariable )
[/lua]

Ah brilliant that worked! Thanks a lot.