Jump to content

JSON problem


Wei

Recommended Posts

local storWeapon = fromJSON(getElementData(housePickup[houseID], "IGChousing.storedweapons")) 
if (type(storWeapon) == "table") then 
        local tableWeapon = storWeapon 
        if tableWeapon[getSlotFromWeapon(weapon)] then 
                tableWeapon[getSlotFromWeapon(weapon)] = weapon..","..tableWeapon[getSlotFromWeapon(weapon)]+ammo 
                setElementData(housePickup[houseID], "IGChousing.storedweapons", toJSON(tableWeapon)) 
                outputChatBox("In") 
        else 
                table.insert(tableWeapon, getSlotFromWeapon(weapon), weapon..","..ammo) 
                setElementData(housePickup[houseID], "IGChousing.storedweapons", toJSON(tableWeapon)) 
                outputChatBox("NotIn") 
        end 

Chatbox allways outputs NotIn.

No debug!

Link to comment
local storWeapon = fromJSON ( getElementData ( housePickup [ houseID ], "IGChousing.storedweapons" ) ) 
if ( type ( storWeapon ) == "table" ) then 
    local tableWeapon = storWeapon 
    if tableWeapon [ getSlotFromWeapon ( weapon ) ) ] then 
        tableWeapon [ getSlotFromWeapon ( weapon ) ] = weapon ..",".. tableWeapon [ getSlotFromWeapon ( weapon ) ] + ammo 
        setElementData ( housePickup [ houseID ], "IGChousing.storedweapons", toJSON ( tableWeapon ) ) 
        outputChatBox ( "In" ) 
    else 
        tableWeapon [ getSlotFromWeapon ( weapon ) ] = weapon ..",".. ammo 
        setElementData ( housePickup [ houseID ], "IGChousing.storedweapons", toJSON ( tableWeapon ) ) 
        outputChatBox ( "NotIn" ) 
    end 

This won't work btw:

 weapon ..",".. tableWeapon [ getSlotFromWeapon ( weapon ) ] + ammo 

You are trying to increase a string...

Link to comment

Got it, you must convert your number to a string.

local storWeapon = fromJSON ( getElementData ( housePickup [ houseID ], "IGChousing.storedweapons" ) ) 
if ( type ( storWeapon ) == "table" ) then 
    local tableWeapon = storWeapon 
    local slot = tostring ( getSlotFromWeapon ( weapon ) ) 
    if tableWeapon [ slot ] then 
        tableWeapon [ slot ] = weapon ..",".. tableWeapon [ slot ] + ammo 
        setElementData ( housePickup [ houseID ], "IGChousing.storedweapons", toJSON ( tableWeapon ) ) 
        outputChatBox ( "In" ) 
    else 
        tableWeapon [ slot ] = weapon ..",".. ammo 
        setElementData ( housePickup [ houseID ], "IGChousing.storedweapons", toJSON ( tableWeapon ) ) 
        outputChatBox ( "NotIn" ) 
    end 

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