Jump to content

Save-System for weapons NOT selected in hand?


Cronoss

Recommended Posts

I can't find any tutorial about something like this but I know it's possible because I searched if this question was made before in the forum and yes, I saw some codes  and explanation from users and mods, but it doesn't work for me, because I'm using mysql and phpmyadmin, the examples doesn't match with my project, so I was wondering if someone could help me to understand how to save the data from the player's weapon and ammo... this is what I tried first:

---------this is inside of a function that saves all the player's info--------
local weapon1 = getPedWeapon (source) 
local weapon = getWeaponNameFromID (weapon) 
local ammo = getPedTotalAmmo(source)
--------the info it's send to the table (this was simplified)-----------
local sendInfo = exports.mysql:_Query("UPDATE characters SET weapon=?, ammo=? WHERE account=?", weapon, ammo, account)

Since i've started, many problems as "getPedWeapons a nill value" appeared in the console, also "expected number, got boolean", etc.

 

If there is a way to make a system like this without the getaccountdata/setaccountdata... I would like to know how to make it. I'm not asking for a full code, just some of knowlegde :)

Link to comment
1 hour ago, Cronoss said:

I can't find any tutorial about something like this but I know it's possible because I searched if this question was made before in the forum and yes, I saw some codes  and explanation from users and mods, but it doesn't work for me, because I'm using mysql and phpmyadmin, the examples doesn't match with my project, so I was wondering if someone could help me to understand how to save the data from the player's weapon and ammo... this is what I tried first:

---------this is inside of a function that saves all the player's info--------
local weapon1 = getPedWeapon (source) 
local weapon = getWeaponNameFromID (weapon) 
local ammo = getPedTotalAmmo(source)
--------the info it's send to the table (this was simplified)-----------
local sendInfo = exports.mysql:_Query("UPDATE characters SET weapon=?, ammo=? WHERE account=?", weapon, ammo, account)

Since i've started, many problems as "getPedWeapons a nill value" appeared in the console, also "expected number, got boolean", etc.

 

If there is a way to make a system like this without the getaccountdata/setaccountdata... I would like to know how to make it. I'm not asking for a full code, just some of knowlegde :)

If you don't give a slot to getPedWeapon, it will return the weapon you have.

so specify the slot you will get

getPedWeapon ( ped thePed, [ int weaponSlot = current ] )

you can use JSON to save the weapons in a mysql column

You can create an inventory column in mysql and then you can use convertWeaponsToJSON at game exit. When you spawned, you can give weapons using giveWeaponsFromJSON 

--this part will be executed when the player exits the game
function convertWeaponsToJSON(player) 
    local weaponSlots = 12 --up to which slot the weapons will be saved
    local weaponsTable = {} --a table to temporarily store weapons
    for slot=0, weaponSlots do 
        local weapon = getPedWeapon(player, slot) 
        local ammo = getPedTotalAmmo(player, slot) 
        if (weapon > 0 and ammo > 0) then 
		        if(weapon == 37) then --for ammo increase bug in this flamethrower
			        ammo = ammo/10
			      end
            weaponsTable[weapon] = ammo --create a reference to the table with the weapon id and ammo amount
        end 
    end
    return toJSON(weaponsTable) --return table as json
end 

--to give weapons from mysql table when this player spawn (just one time)  
function giveWeaponsFromJSON(player, weapons) 
    if (weapons and weapons ~= "") then
        takeAllWeapons(player) --take all weapons before the player. To avoid the ammo increase bug
        for weapon, ammo in pairs(fromJSON(weapons)) do  --convert json format to table and load weapons into player
            if (weapon and ammo) then 
                giveWeapon(player, tonumber(weapon), tonumber(ammo)) --give weapon to player
            end 
        end
    end 
end 

 

Edited by Burak5312
  • Thanks 1
Link to comment

I think i'm misunderstanding what you telling me to do...

Quote

You can create an inventory column in mysql and then you can use convertWeaponsToJSON at game exit. 

 

I tried something but i'm doing it wrong:

function recordarData()
local playerAccount = getPlayerAccount(source)
	if(playerAccount) then
	end
	local x, y, z = getElementPosition(source)
	local money = getPlayerMoney(source)
	local health = getElementHealth(source)
	local dimension = getElementDimension(source) 
	local interior = getElementInterior(source)
	local skin = getElementModel(source)
	local account = getAccountName(playerAccount)
	local xr, yr, zr = getElementRotation(source)
	local weapon = convertWeaponsToJSON(source) ---------here
	local sendInfo = exports.mysql:_Query("UPDATE characters SET x=?, y=?, z=?, money=?, health=?, dimension=?, interior=?, skin=?, xr=?, yr=?, zr=?, weapon=? WHERE account=?", x, y, z, money, health, dimension, interior,skin,xr,yr,zr,weapon, account) -----send the weapon info
	if (sendInfo) then
		iprint("Data saved") ------this shows up in the console
	else
		iprint("Error saving")
	end
end
addEventHandler("onPlayerQuit", getRootElement(), recordarData)

I created "weapon" in the character's table so It should save the info from the function and send it to phpmyadmin but I just checked and nothing happens

When player login:

giveWeaponsFromJSON(source, column["weapon"]) -----this is just the weapon part

If i'm doing it in the wrong way I apologize for making the message too long

 

note: I obviusly added the function "giveweaponsfromjson" and the other one, so that's not the problem

Edited by Cronoss
Link to comment

Well, the console sent me the error in this part:

for weapon, ammo in pairs(fromJSON(weapons)) do  

Something like "the table doesn't exist"... that's why I thought I made it wrong. But i don't know if it would work with that part fixed, because, as i said, the table doesn't get any info in the weapon column, it keeps empty

 

-----

And, my answers for your questions:

1.- Yes, empty all the time

2.- I'll be checking that later, because now I'm leaving my house

3.- Yes, the "giveWeaponsFromJSON(source, column["weapon"])" it's after the spawn and all that stuff for the player, here it's the full code:

function cargarDatos()
	local playerAccount = getPlayerAccount(source)
	if(playerAccount) then
        if(isGuestAccount(playerAccount)) then 
        return 
   		end 
	end
	local accName = getAccountName(playerAccount)
		local setData = exports.mysql:_Query("SELECT * FROM characters WHERE cuenta=? LIMIT 1", accName)
	if (setData) then
		if(#setData > 0) then
			for _,column in ipairs(setData) do
				setPlayerMoney(source, column["money"])
				setPlayerName(source, column["name"])
				setCameraTarget(source)
				showCursor(source, false)
				spawnPlayer(source, column["x"], column["y"], column["z"]) ---------spawn
				setElementDimension(source, column["dimension"])
				setElementInterior(source, column["interior"])
				setElementRotation(source, column["xr"], column["yr"], column["zr"])
				setElementModel(source, column["skin"])
				setElementHealth(source, column["health"])
        		giveWeaponsFromJSON(source, column["weapon"]) ---------------Weapons
				break
			end
		end
	end
end

 

Edited by Cronoss
Link to comment

Try using the giveWeaponsFromJSON function inside the "onPlayerSpawn" event

other than that make sure the column type is VARCHAR and has sufficient length

To test whether the weapon gives or not, you can enter the column manually

[{ "27": 1500, "22": 1500, "28": 1500 } ]

Apart from that, I tested both functions on my own computer, there is no problem.

Edited by Burak5312
  • Thanks 1
Link to comment

The column in the table was specified with "INT" instead of "VARCHAR", thank you for your help Burak!  it's a little confusing to me...  Why sometimes it's VARCHAR and sometimes "INT" ? 

my columns with INT:

Position, dimension, interior, rotation...

my columns with VARCHAR:

names, texts, name of the account...

but why this (weapon's table) have to be varchar? just asking 

 

EDIT: I know int means integer, but why in this case the int doesn't work?

Edited by Cronoss
  • Like 1
Link to comment
Quote

but why this (weapon's table) have to be varchar? just asking

 I know int means integer, but why in this case the int doesn't work?

Why sometimes it's VARCHAR and sometimes "INT" ? 

json returns a string type, so when saving, we should do it as VARCHAR in mysql, I'm not sure, but this string may be equivalent to mysql

example:

addCommandHandler("weap", function(player)
   local weapon =  '[{ "27": 1500, "22": 1500, "28": 1500 } ]' --As you can see here json data is string
   giveWeaponsFromJSON(player, weapon)
end)

You can check the return value of the functions you use to distinguish it from the wiki, for example, the getPlayerName function returns a string on success.

--here
string getPlayerName ( player thePlayer )

likewise the setPlayerName function takes a string of data

                                        --here
bool setPlayerName ( player thePlayer, string newName )

so the player's name is always string

By looking at the return values of the functions you use, you can understand what should be INT and what should be VARCHAR.

Edited by Burak5312
  • 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...