DRW Posted July 15, 2015 Share Posted July 15, 2015 (edited) Hello, I've created some peds and I don't want them to get shot by slothbots, I've set some slothbot-related elementdatas to the peds but it's not working, also, I've tried to modify the original script but I do always manage to break it instead of making it how I want it to be, what can I do? Note: I just want to make bots attack ONLY players. Slothbot serverside STUFF --when a bot dies, tells the bot what to do next function peddeath(ammo, attacker, weapon, bodypart) if (getElementData (source, "slothbot") == true) then -- if its a slothbot triggerEvent ( "onBotWasted", source, attacker, weapon, bodypart ) if (attacker) then if getElementType (attacker) == "player" then --nothing end end setElementData ( source, "status", "dead" ) setTimer ( destroyElement, 5000, 1, source) local peds = getElementsByType ( "ped" ) for theKey,thePed in ipairs(peds) do if (getElementData ( thePed, "leader" ) == source ) then if (getElementData ( thePed, "status" ) == "following" ) then setElementData ( thePed, "leader", nil ) setTimer (beginsearch, 800, 1, thePed ) -- move on else setElementData ( thePed, "leader", nil ) end end if (getElementData ( thePed, "target" ) == source ) then setElementData ( thePed, "target", nil ) end end else -- if its a non slothbot ped if (attacker) then if (getElementType(attacker) == "ped") and (getElementData (attacker, "slothbot") == true) then --nothing (might add something later) end end end end addEventHandler("onPedWasted", getRootElement(), peddeath) --when a player dies, tells the bot what to do next function playerdeath( ammo, attacker, weapon, bodypart ) if (attacker) then if (getElementData (attacker, "slothbot") == true) then --nothing end end local peds = getElementsByType ( "ped" ) for theKey,thePed in ipairs(peds) do if (getElementData (thePed, "slothbot") == true) then if (getElementData ( thePed, "leader" ) == source ) then if (getElementData ( thePed, "status" ) == "following" ) then setTimer (beginsearch, 800, 1, thePed ) -- move on to the next target setElementData ( thePed, "leader", nil ) else setElementData ( thePed, "leader", nil ) end end if (getElementData ( thePed, "target" ) == source ) then setElementData ( thePed, "target", nil ) end end end end addEventHandler("onPlayerWasted", getRootElement(), playerdeath) --when a bot sees an enemy addEvent( "onBotFindEnemy", true ) function assigntarget ( player ) if ( not wasEventCancelled() ) then if (isElement(player)) and (getElementData (source, "slothbot") == true) then if (getElementData ( source, "target" ) ~= player ) and (getElementType ( player ) == "player") then setElementData ( source, "target", player ) if (getElementData ( source, "status" ) ~= "chasing" ) then assigncontroller(source) setElementData ( source, "status", "chasing" ) end elseif (getElementData ( source, "target" ) ~= player ) and (getElementType ( player ) == "ped") then setElementData ( source, "target", player ) if (getElementData ( source, "status" ) ~= chasing ) then assigncontroller(source) setElementData ( source, "status", "chasing" ) end end end end end addEventHandler( "onBotFindEnemy", getRootElement(), assigntarget ) --initializes the ped to start walking the paths addEvent( "onHuntStart", true ) function beginsearch (theped) if (isElement(theped)) then if (getElementData (theped, "slothbot") == true) then setElementData ( theped, "target", nil ) setElementData ( theped, "leader", nil ) assigncontroller(theped) setElementData ( theped, "status", "hunting" ) end end end addEventHandler( "onHuntStart", getRootElement(), beginsearch ) --detects what mode the ped is changed to and activates the correct behaviour (hunting, shooting, following, etc) function changestatus (dataName) if getElementType ( source ) == "ped" and dataName == "status" and (getElementData (source, "slothbot") == true) then if (getElementData ( source, "status" ) ~= "stasis" ) then local allshapes = getElementsByType ( "colshape" ) for SKey,theShape in ipairs(allshapes) do if (isElement(theShape)) then if getElementParent (theShape) == source then destroyElement (theShape) -- removes the target colshape that the ped was running towards in hunt mode end end end end if (getElementData ( source, "status" ) == "chasing" ) then local player = getElementData ( source, "target" ) local oldTx, oldTy, oldTz = getElementPosition( player ) local oldPx, oldPy, oldPz = getElementPosition( source ) setTimer ( chase_move, 700, 1, source, oldTx, oldTy, oldTz, oldPx, oldPy, oldPz ) elseif (getElementData ( source, "status" ) == "hunting" ) then setTimer ( findPath, 200, 1, source ) elseif (getElementData ( source, "status" ) == "waiting" ) then setTimer ( wait_mode, 200, 1, source ) elseif (getElementData ( source, "status" ) == "following" ) then local player = getElementData ( source, "leader" ) local oldTx, oldTy, oldTz = getElementPosition( player ) local oldPx, oldPy, oldPz = getElementPosition( source ) setTimer ( follow_move, 700, 1, source, oldTx, oldTy, oldTz, oldPx, oldPy, oldPz ) elseif (getElementData ( source, "status" ) == "guarding" ) then local px,py,pz = getElementPosition( source ) guard_move (source, px, py, pz) end end end addEventHandler( "onElementDataChange", getRootElement(), changestatus ) -- tells the bot how to chase enemies function chase_move (ped, oldTx, oldTy, oldTz, oldPx, oldPy, oldPz) if (isElement(ped)) then if (getElementData ( ped, "status" ) == "chasing" ) and (getElementData (ped, "slothbot") == true) then local px,py,pz = getElementPosition( ped ) local player = getElementData ( ped, "target" ) if (isElement(player)) then local tx,ty,tz = getElementPosition( player ) local pedhp = getElementHealth ( ped ) if getElementType(player) == "player" then if pedhp > 0 and (isPedDead(player) == false) then if (getElementData ( ped, "seestarget" ) == true ) then-- if the ped can see the target local inrange = 0 local tdist = (getDistanceBetweenPoints3D (px, py, pz, tx, ty, tz)) if (getPedWeaponSlot(ped) == 0 ) then -- fists triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 elseif (getPedWeaponSlot(ped) == 1 ) then -- melee triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 elseif (getPedWeaponSlot(ped) == 2 ) then -- pistols if tdist < 14 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 3 ) then -- shotguns if tdist < 10 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 4 ) then -- submachine if tdist < 7 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 5 ) then -- assault if tdist < 14 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 6 ) then --rifles if tdist < 22 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 7 ) then --heavy if tdist < 12 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 8 ) then --projectiles (dont work afaik) triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 elseif (getPedWeaponSlot(ped) == 9 ) then -- special if tdist < 2 then triggerClientEvent ( "bot_Stop", ped ) inrange = 1 else triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end elseif (getPedWeaponSlot(ped) == 10 ) then -- gifts triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 elseif (getPedWeaponSlot(ped) == 11 ) then -- special2 triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 elseif (getPedWeaponSlot(ped) == 12 ) then -- detonator triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 else -- probably useless part. triggerClientEvent ( "bot_Forwards", ped ) inrange = 0 end local pdistance = (getDistanceBetweenPoints3D (oldPx, oldPy, oldPz, px, py, pz)) if (pdistance < 1 ) and (inrange == 0 ) then -- if the ped is stuck but not in range of the player local weap = getPedWeaponSlot(ped) if (weap == 1) or (weap == 7) then setPedWeaponSlot(ped, 0 ) triggerClientEvent ( "bot_Jump", ped ) setTimer ( setPedWeaponSlot, 850, 1, ped, weap) else triggerClientEvent ( "bot_Jump", ped ) end setTimer ( chase_move, 600, 1, ped, tx, ty, tz, px, py, pz) else setTimer ( chase_move, 600, 1, ped, tx, ty, tz, px, py, pz) end else -- if the ped cant see the target triggerClientEvent ( "bot_Forwards", ped ) local pdistance = (getDistanceBetweenPoints3D (oldPx, oldPy, oldPz, px, py, pz)) if (pdistance < 1.2 ) then --if the ped is stuck local decide = math.random( 1, 7 ) -- randomly decide to if decide == 1 then -- give up chasing and go back on the hunt setTimer (beginsearch, 800, 1, ped ) elseif decide <4 then -- jump Edited July 15, 2015 by Guest Link to comment
DRW Posted July 15, 2015 Author Share Posted July 15, 2015 Due to character limits I had to split clientside and serverside into 2 posts, sorry Slothbot clientside STUFF --detects when a bot changes status and starts the new behaviour function changestatus (dataName) if getElementType ( source ) == "ped" and dataName == "status" and (getElementData (source, "slothbot") == true) then if (getElementData ( source, "status" ) == "chasing" ) then local player = getElementData ( source, "target" ) local oldTx, oldTy, oldTz = getElementPosition( player ) local oldPx, oldPy, oldPz = getElementPosition( source ) setTimer ( chase_aim, 80, 1, source) setTimer ( chase_move, 700, 1, source, oldTx, oldTy, oldTz, oldPx, oldPy, oldPz ) setTimer ( chase_shoot, 1500, 1, source ) elseif (getElementData ( source, "status" ) == "hunting" ) then local oldPx, oldPy, oldPz = getElementPosition( source ) setTimer ( enemy_check, 600 ,1 , source )-- starts scanning for enemies setTimer ( hunt_move, 800, 1, source, oldPx, oldPy, oldPz, oldPx, oldPy, oldPz ) elseif (getElementData ( source, "status" ) == "following" ) then local player = getElementData ( source, "leader" ) local oldTx, oldTy, oldTz = getElementPosition( player ) local oldPx, oldPy, oldPz = getElementPosition( source ) setTimer ( follow_enemy_check, 500 ,1 , source ) setTimer ( follow_move, 800, 1, source, oldPx, oldPy, oldPz, oldPx, oldPy, oldPz ) elseif (getElementData ( source, "status" ) == "waiting" ) then setTimer ( wait_enemy_check, 500 ,1 , source ) elseif (getElementData ( source, "status" ) == "guarding" ) then setTimer ( guard_enemy_check, 800 ,1 , source )-- starts scanning for enemies setTimer ( guard_move, 400 ,1 , source ) end end end addEventHandler( "onClientElementDataChange", getRootElement(), changestatus ) --keeps the bot's aim slightly lagged function chase_aim(ped) if (isElement(ped)) then local player = getElementData ( ped, "target" ) if (getElementData ( ped, "status" ) == "chasing" ) and (isElement(player)) and (getElementData (ped, "slothbot") == true) then if getElementType(player) == "player" then local pedhp = getElementHealth ( ped ) if pedhp > 0 and (isPlayerDead(player) == false) then local x,y,z = getElementPosition( player ) local ex,ey,ez = getElementPosition( ped ) local isclear = isLineOfSightClear (ex, ey, ez+.6, x, y, z+.6, true, false, false, true, false, false, false) if (isclear == true) then -- if the player target is in sight if isPedDucked(player) then setPedAimTarget ( ped, x, y, z-.5 ) else setPedAimTarget ( ped, x, y, z ) end end setTimer ( chase_aim, 80, 1, ped, player ) end elseif getElementType(player) == "ped" then local pedhp = getElementHealth ( ped ) if pedhp > 0 and (getElementHealth(player) > 0) then local x,y,z = getElementPosition( player ) local ex,ey,ez = getElementPosition( ped ) local isclear = isLineOfSightClear (ex, ey, ez+.6, x, y, z+.6, true, false, false, true, false, false, false) if (isclear == true) then -- if the player target is in sight if isPedDucked(player) then setPedAimTarget ( ped, x, y, z-.5 ) else setPedAimTarget ( ped, x, y, z ) end end setTimer ( chase_aim, 80, 1, ped, player ) end end end end end --bot behaviour while in chase mode function chase_move (ped, oldTx, oldTy, oldTz, oldPx, oldPy, oldPz) if (isElement(ped)) then local thetarget = getElementData ( ped, "target" ) if (getElementData ( ped, "status" ) == "chasing" ) and (isElement(thetarget)) and (getElementData (ped, "slothbot") == true) then local pedhp = getElementHealth ( ped ) local tarhp = getElementHealth ( thetarget ) if pedhp > 0 and tarhp > 0 then local tx,ty,tz = getElementPosition( thetarget ) local px,py,pz = getElementPosition( ped ) local isclear = isLineOfSightClear (px, py, pz+.6, tx, ty, tz+.6, true, false, false, true, false, false, false) local angle = ( 360 - math.deg ( math.atan2 ( ( tx - px ), ( ty - py ) ) ) ) % 360 --set the peds angle to the target setPedRotation( ped, angle ) if (isclear == true) then -- if the ped can see the target local angle = ( 360 - math.deg ( math.atan2 ( ( tx - px ), ( ty - py ) ) ) ) % 360 --set the peds angle to the target setPedRotation( ped, angle ) setElementData ( ped, "seestarget", true ) setTimer ( chase_move, 700, 1, ped, tx, ty, tz, px, py, pz) else local angle = ( 360 - math.deg ( math.atan2 ( ( oldTx - px ), ( oldTy - py ) ) ) ) % 360 --set the peds angle to the target setPedRotation( ped, angle ) setElementData ( ped, "seestarget", false ) setTimer ( chase_move, 700, 1, ped, oldTx, oldTy, oldTz, px, py, pz) end end end end end --tells the bot when to fire its gun. (only run by the peds controller) function chase_shoot(ped) if (isElement(ped)) then local player = getElementData ( ped, "target" ) if (isElement(player)) and (getElementData (ped, "slothbot") == true) then if (getElementType(player) == "ped") or (getElementType(player) == "player") then if (getElementData ( ped, "status" ) == "chasing" ) and (isElement(player)) then if (getElementHealth(ped)>0) and (getElementHealth(player)>0) and (getElementData ( ped, "controller" ) == getLocalPlayer()) then local x,y,z = getElementPosition( player ) local ex,ey,ez = getElementPosition( ped ) local isclear = isLineOfSightClear (ex, ey, ez+1, x, y, z+1, true, false, false, true, false, false, false) if isclear == true then -- if the ped can see the local player local distance = (getDistanceBetweenPoints3D (ex, ey, ez, x, y, z)) if (getPedWeapon(ped) < 19) and (getPedWeapon(ped) ~= 9) then -- if its a melee weapon hat isnt a chainsaw if (distance < 2 ) then triggerServerEvent ( "pedShootTrigger", ped, "melee" ) setTimer ( chase_shoot, 2300, 1, ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 9) then -- if it is a chainsaw (watch out, the peds are deadly with these) if (distance < 2 ) then local length = 1500 triggerServerEvent ( "pedShootTrigger", ped, "chainsawA", length ) setTimer ( chase_shoot, 6000, 1, ped, player ) else local length = math.random( 2100, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "chainsawB", length ) setTimer ( chase_shoot, length+900 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 22) then if (distance < 35 ) then local length = math.random( 2100, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+500 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 23) then if (distance < 35 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+400 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 24) then if (distance < 35 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+400 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 25) then if (distance < 27 ) then local length = 300 triggerServerEvent ( "pedShootTrigger", ped, "gun", length) setTimer ( chase_shoot, 1400 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 26) then if (distance < 20 ) then local length = 1200 triggerServerEvent ( "pedShootTrigger", ped, "gun", length) setTimer ( chase_shoot, 800 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 27) then if (distance < 25 ) then local length = 1200 triggerServerEvent ( "pedShootTrigger", ped, "gun", length) setTimer ( chase_shoot, 1800 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 28) then if (distance < 40 ) then local length = math.random( 2100, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+500 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 29) then if (distance < 55 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+1500 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 30) then if (distance < 60 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+1500 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 31) then if (distance < 60 ) then local length = 700 triggerServerEvent ( "pedShootTrigger", ped, "gun", length) setTimer ( chase_shoot, 1200 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 32) then if (distance < 30 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+600 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 33) then if (distance < 75 ) then local length = 400 triggerServerEvent ( "pedShootTrigger", ped, "gun", length) setTimer ( chase_shoot, 1600 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 34) then if (distance < 75 ) then triggerServerEvent ( "pedShootTrigger", ped, "sniper") setTimer ( chase_shoot, 2000 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 37) then if (distance < 15 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+900 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif (getPedWeapon(ped) == 38) then if (distance < 65 ) then local length = math.random( 2000, 5500 ) triggerServerEvent ( "pedShootTrigger", ped, "gun", length ) setTimer ( chase_shoot, length+900 ,1 , ped, player ) else setTimer ( chase_shoot, 1500 ,1 , ped, player ) end elseif ( Link to comment
GTX Posted July 15, 2015 Share Posted July 15, 2015 How about if you set bots to one team, others to another team? https://wiki.multitheftauto.com/wiki/Sl ... setBotTeam Link to comment
DRW Posted July 15, 2015 Author Share Posted July 15, 2015 How about if you set bots to one team, others to another team? https://wiki.multitheftauto.com/wiki/Sl ... setBotTeam But I can't set normal peds to be at the same team as the bots' team, I've tried to use the export function setBotTeam to a normal ped but it obviously didn't work, then I tried to set slothbot-related elementdatas to the original peds (Because the ped bots aren't really in a team, they just have an elementdata that makes them act differently if they find a bot or player of the same team). Link to comment
GTX Posted July 15, 2015 Share Posted July 15, 2015 Try this: function onBotFindEnemy(ped) if isElement(ped) and getElementType(ped) ~= "player" then cancelEvent() end end addEvent("onBotFindEnemy", true) addEventHandler("onBotFindEnemy", root, onBotFindEnemy) It must work. Link to comment
DRW Posted July 15, 2015 Author Share Posted July 15, 2015 Tried that before, nothing happens. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now