Lua, trying to conver a little clothing script

hii i trying to convert this script to a sql connecting.

so as far i did i add the sql thing. and my server error is

server.lua:39: attempt to index a nil value (global 'models')
models = {identifier}
RegisterServerEvent("clothes:firstspawn")
AddEventHandler("clothes:firstspawn",function()
	local source = source
	local identifier = getID("steam",source)
	print(identifier)
	if models[identifier] ~= nil then
		TriggerClientEvent("clothes:spawn", source, models[identifier])
	else
		local default_models = {1413662315,-781039234,1077785853,2021631368,1423699487,1068876755,2120901815,-106498753,131961260,-1806291497,1641152947,115168927,330231874,-1444213182,1809430156,1822107721,2064532783,-573920724,-782401935,808859815,-1106743555,-1606864033,1004114196,532905404,1699403886,-1656894598,1674107025,-88831029,-1244692252,951767867,1388848350,1090617681,379310561,-569505431,-1332260293,-840346158}
		models[identifier] = {
			model = default_models[math.random(1,tonumber(#default_models))],
			new = true,
			clothing = {drawables = {0,0,0,0,0,0,0,0,0,0,0,0},textures = {2,0,1,1,0,0,0,0,0,0,0,0},palette = {0,0,0,0,0,0,0,0,0,0,0,0}},
			props = {drawables = {-1,-1,-1,-1,-1,-1,-1,-1}, textures = {-1,-1,-1,-1,-1,-1,-1,-1}},
			overlays = {drawables = {-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, opacity = {1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0}, colours = {{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0},{colourType = 0, colour = 0}}},
		}
		saveModels()
		TriggerClientEvent("clothes:spawn", source, models[identifier])
	end
end)

RegisterServerEvent("clothes:spawn")
AddEventHandler("clothes:spawn",function()
	local source = source
	local identifier = getID("steam",source)
	TriggerClientEvent("clothes:spawn", source, models[identifier])
end)

RegisterServerEvent("clothes:loaded")
AddEventHandler("clothes:loaded",function()
	-- Give weapons etc
end)

RegisterServerEvent("clothes:save")
AddEventHandler("clothes:save",function(player_data)
	local source = source
	local identifier = getID("steam",source)
	models[identifier] = player_data
	saveModels()
	-- Give weapons etc
end)

function loadModels()
    models = LoadResourceFile(GetCurrentResourceName(), "models.txt") or "[]"
    models = json.decode(models)
end

function saveModels()
	MySQL.Sync.execute("UPDATE modelmenu SET mpmodel = 0,model=@model, mask=@mask, mask_txt=@mtxt, shirt=@shirt, shirt_txt=@stxt, shoe=@shoe, shoe_txt=@shtxt, pants=@pants, pants_txt=@ptxt, helmet=@helmet, helmet_txt=@htxt, glasses=@glasses, glasses_txt=@gtxt, percing=@piercing, percing_txt=@petxt WHERE identifier=@user",{['@model']= model,['@mask']= user.mask,['@mtxt']= user.mask_txt,['@shirt']= user.shirt,['@stxt']= user.shirt_txt,['@shoe']= user.shoe,['@shtxt']= user.shoe_txt,['@pants']= user.pants,['@ptxt']= user.pants_txt,['@helmet']= user.helmet,['@htxt']= user.helmet_txt,['@glasses']= user.glasses,['@gtxt']= user.glasses_txt,['@piercing']= user.piercing,['@petxt']= user.piercing_txt,['@user']= identifier})
end

function getID(type, source)
    for k,v in ipairs(GetPlayerIdentifiers(source)) do
        if string.sub(tostring(v), 1, string.len("steam:")) == "steam:" and (type == "steam" or type == 1) then
            return v
        elseif string.sub(tostring(v), 1, string.len("license:")) == "license:" and (type == "license" or type == 2) then
            return v
        elseif string.sub(tostring(v), 1, string.len("ip:")) == "ip:" and (type == "ip" or type == 3) then
            return v
        end
    end
    return nil
end

loadModels()

line 39 The error

RegisterServerEvent("clothes:save")
AddEventHandler("clothes:save",function(player_data)
	local source = source
	local identifier = getID("steam",source)
	**models[identifier] = player_data** this is the error...
	saveModels()
	-- Give weapons etc
end)

Please post in the correct category. This helps keep the forums clean and tidy. It might also help you!

If your unsure of where to post or make a topic refer to this post.

Moved to: #development:scripts

1 Like

okay got you, thanks sir!

1 Like

Hi,

Here:

MySQL.Sync.execute("UPDATE modelmenu SET mpmodel = 0,model=@model, mask=@mask, mask_txt=@mtxt, shirt=@shirt, shirt_txt=@stxt, shoe=@shoe, shoe_txt=@shtxt, pants=@pants, pants_txt=@ptxt, helmet=@helmet, helmet_txt=@htxt, glasses=@glasses, glasses_txt=@gtxt, percing=@piercing, percing_txt=@petxt WHERE identifier=@user",{['@model']= model,['@mask']= user.mask,['@mtxt']= user.mask_txt,['@shirt']= user.shirt,['@stxt']= user.shirt_txt,['@shoe']= user.shoe,['@shtxt']= user.shoe_txt,['@pants']= user.pants,['@ptxt']= user.pants_txt,['@helmet']= user.helmet,['@htxt']= user.helmet_txt,['@glasses']= user.glasses,['@gtxt']= user.glasses_txt,['@piercing']= user.piercing,['@petxt']= user.piercing_txt,['@user']= identifier})

model, user and identifier doesn’t exist here.

hi i tryed to fix this and didnt success. is there a way you are trying to help me out

here is the new code i make

function saveModels()
	MySQL.Async.execute("UPDATE modelmenu SET mpmodel = 0,models=@models, mask=@mask, mask_txt=@mtxt, shirt=@shirt, shirt_txt=@stxt, shoe=@shoe, shoe_txt=@shtxt, pants=@pants, pants_txt=@ptxt, helmet=@helmet, helmet_txt=@htxt, glasses=@glasses, glasses_txt=@gtxt, percing=@piercing, percing_txt=@petxt WHERE identifier=@player",{['@models']= model,['@mask']= player.mask,['@mtxt']= player.mask_txt,['@shirt']= player.shirt,['@stxt']= player.shirt_txt,['@shoe']= player.shoe,['@shtxt']= player.shoe_txt,['@pants']= player.pants,['@ptxt']= player.pants_txt,['@helmet']= player.helmet,['@htxt']= player.helmet_txt,['@glasses']= player.glasses,['@gtxt']= player.glasses_txt,['@piercing']= player.piercing,['@petxt']= player.piercing_txt,['@player']= identifier})
end

I can’t help you a lot as long as I can’t be sure where player components are (user.shirt, user.shirt_txt, …). By looking in your code, I think it’s in player_data, but you’ll need to parse everything (not the best), change your database or change the client side parsing. Here, you’ll need lua experience.

I suppose you haven’t a lots of experience about that, so you can maybe use another resource or find a developer to help you.

Personaly, I like to help people, but I don’t have time to completely rework a resource.

1 Like

thanks very much, for helping me <3 it was a very good comment btw.
thanks bro i take care about what you are saying to me and i will try to make some changes and figure out how to fix it.