Hi. This is my code. Everything works, except part where NPC's are given weapons. Animations also doesn't work. It even returns getPedAnimation or getPedWeaponSlot correctly, but no effect. Any help, please?
Server side script, obviously. Nothing is being linked to client side script, or vice versa.
mysql = exports.mysql
function loadOnePed(id)
local query = mysql:query_fetch_assoc("SELECT * FROM npcs WHERE id = '"..mysql:escape_string(id).."'")
ped = createPed (query["skinID"], query["x"], query["y"], query["z"])
setPedRotation(ped, query["rot"])
exports.pool:allocateElement(ped, tonumber(query["id"]))
setElementFrozen(ped, true)
setElementInterior(ped, query["interior"])
setElementInterior(ped, query["dimension"])
setElementData(ped, "dbid", query["id"])
setElementData(ped, "created", "true")
setElementData(ped, "skin", query["skinID"])
local weapon = tonumber(query["weapon"])
if weapon > 0 then
giveWeapon(ped, weapon, 30, true)
--outputChatBox(getPedWeaponSlot(ped))
end
setPedAnimation(ped, query["anim_block"], query["anim_id"], -1, true, false, false, false)
end
function nearbyPeds(thePlayer, commandName)
if getElementData(thePlayer, "adminlevel") > 1 then
outputChatBox("Nearby Peds:", thePlayer, 255, 126, 0)
local count = 0
for index, thePed in ipairs( exports.global:getNearbyElements(thePlayer, "ped") ) do
if getElementData(thePed, "created") == "true" then
outputChatBox(" PED (ID: #"..getElementData(thePed, "dbid")..") Skin: #"..getElementData(thePed, "skin"), thePlayer, 255, 126, 0)
count = count + 1
end
end
if count == 0 then
outputChatBox(" None.", thePlayer, 255, 126, 0)
end
end
end
addCommandHandler("np", nearbyPeds)
function delPed(thePlayer, commandName, id)
if getElementData(thePlayer, "adminlevel") > 3 then
if not id then
outputChatBox("SYNTAX: /dp id", thePlayer)
else
thePed = exports.pool:getElement("ped", tonumber(id))
outputChatBox(thePed, "dbid")
destroyElement(thePed)
local q = mysql:query("UPDATE npcs SET deleted = 1 WHERE id = '"..mysql:escape_string(id).."'")
--local q = mysql:query("DELETE FROM npcs WHERE id = '"..mysql:escape_string(id).."'")
if q then
outputChatBox("NPC #"..id.." deleted!", thePlayer, 255, 126, 0)
end
end
end
end
addCommandHandler("dp", delPed)
function addNewNPC(thePlayer, commandName, skinID, x, y, z, rot, interior, dimension, anim_block, anim_id, weapon)
if getElementData(thePlayer, "adminlevel") > 1 then
if not skinID or not x or not y or not z or not interior or not dimension then
outputChatBox("SYNTAX: /newnpc skinID x y z rot interior dimension anim_block anim_id weapon_id", thePlayer)
else
local id = SmallestID()
local skinID = mysql:escape_string(skinID)
local x = mysql:escape_string(x)
local y = mysql:escape_string(y)
local z = mysql:escape_string(z)
local rot = mysql:escape_string(rot)
local interior = mysql:escape_string(interior)
local dimension = mysql:escape_string(dimension)
--[[if not weapon then
local weapon = 0
end
if not anim_block or not anim_id then
local anim_block = 0
local anim_id = 0
end
local anim_block = mysql:escape_string(anim_block)
local anim_id = mysql:escape_string(anim_id)--]]
local weapon = mysql:escape_string(weapon)
local query = mysql:query_insert_free("INSERT INTO npcs (id, skinID, x, y, z , rot, interior, dimension, weapon, anim_block, anim_id) VALUES ('"..id.."', '"..skinID.."', '"..x.."', '"..y.."','"..z.."','"..rot.."','"..interior.."','"..dimension.."', '"..weapon.."', '"..anim_block.."','"..anim_id.."') ")
if query then
loadOnePed(tonumber(id))
outputChatBox("NPC #"..id.." created!", thePlayer, 255, 126, 0)
end
end
end
end
addCommandHandler("newnpc", addNewNPC)
function loadAllPeds()
local result = mysql:query("SELECT id FROM `npcs` WHERE deleted = 0 ORDER BY `id` ASC")
if result then
while true do
local row = mysql:fetch_assoc(result)
if not row then break end
loadOnePed(tonumber(row["id"]))
end
end
end
addEventHandler("onResourceStart", getRootElement(), loadAllPeds)
function SmallestID()
local result = mysql:query_fetch_assoc("SELECT MIN(e1.id+1) AS nextID FROM npcs AS e1 LEFT JOIN npcs AS e2 ON e1.id +1 = e2.id WHERE e2.id IS NULL")
if result then
local id = tonumber(result["nextID"]) or 1
return id
end
return false
end