News: Welcome back to Bullworth! If you haven't already, you will need to reset your password..


Author Topic: Lua PedSetWeapon & PedSetHealth  (Read 5166 times)

0 Members and 1 Guest are viewing this topic.

Offline NamesStephen

  • Jr. Member
  • **
  • Posts: 4
  • Gender: Male
  • A man with a lot of time to waste.
    • View Profile
Lua PedSetWeapon & PedSetHealth
« on: November 03, 2020, 12:10:55 AM »
I'm currently learning lua scripting, and I've run into a problem I just don't know how to fix. I'm trying to spawn Johnny with a bat, and with 200 health. I've just been messing around so I have know idea what I am doing, I'm into that self teaching kinda thing, ya know.

Heres what I have so far.

function MissionSetup()
   PlayerSetPosXYZArea(270,-110,6,0)
   PlayerSetHealth(300)
end

function main()
   PedSetWeapon(23,300,1)
   PedSetHealth(23,2000)
   PedSetHealth(21,33)
   PedSetWeapon(23,395,1)
  TextPrintString("Defeat the Greasers", 5, 1)
   enemy = PedCreateXYZ(23,265,-110,6)
   PedSetPedToTypeAttitude(enemy,13,0)
   PedAttackPlayer(enemy,3)
   enemy = PedCreateXYZ(21,265,-110,6)
   PedSetPedToTypeAttitude(enemy,13,0)
   PedAttackPlayer(enemy,3)
   
   repeat
      Wait(0)
   until PedIsDead(enemy)
   MissionSucceed()
end




One more question I've been wondering about is does the spacing matter in sentences or not?

Offline SimonBestia

  • Full Member
  • ***
  • Posts: 294
  • Gender: Male
  • Bully-Board's Best Weeb
    • View Profile
    • Youtube Channel
Re: Lua PedSetWeapon & PedSetHealth
« Reply #1 on: November 03, 2020, 10:17:13 AM »
There's a couple of issues with your code. Let me help:

  • Always spawn Jimmy in any function but MissionSetup. That usually gives issues.
  • Use AreaTransitionXYZ(Area, X, Y, Z) instead of PlayerSetPosXYZArea. It's a better function.
  • You CAN'T control peds universally via their IDs. Instead, you have to create an enemy every time, if you want to control them independently.
  • You have two different peds called "enemy". That will confuse the game, so change the name to something else. (LUA is case-sensitive so even enemy and Enemy can work independently)

Spacing doesn't matter, but having a clean-looking code feels good.

I tried to rewrite your script so try and learn from this:
Code: [Select]
function MissionSetup()

PlayerSetHealth(300) --This could be moved to main since there's no point in having an entire MissionSetup function to do just one thing, but you decide. (MissionSetup is not mandatory, but if present, it will be the first function read by the game)

end

function main()

AreaTransitionXYZ(0, 270, -110, 6)
Johnny = PedCreateXYZ(23, YourX, YourY, YourZ) --Replace the cords for both peds. (duh)
Peanut = PedCreateXYZ(21, YourX, YourY, YourZ)
PedSetWeapon(Johnny, 300, 1)
PedSetWeapon(Peanut, 395, 1)
PedSetHealth(Johnny, 2000)
PedSetHealth(Peanut, 33)
TextPrintString("Defeat the Greasers", 5, 1)
PedSetPedToTypeAttitude(Johnny, 13, 0)
PedSetPedToTypeAttitude(Peanut, 13, 0)
PedAttackPlayer(Johnny, 3)
PedAttackPlayer(Peanut, 3)

repeat
Wait(0)
until PedIsDead(Johnny) and PedIsDead(Peanut)

MissionSucceed()

end

Random extra tip:
Since you're changing the attitude of two greaser peds, instead of doing PedSetPedToTypeAttitude twice, you could just do PedSetTypeToTypeAttitude(Faction1, Faction2, Attitude) once. (Unless, again, you want to control those two independently)

Offline NamesStephen

  • Jr. Member
  • **
  • Posts: 4
  • Gender: Male
  • A man with a lot of time to waste.
    • View Profile
Re: Lua PedSetWeapon & PedSetHealth
« Reply #2 on: November 04, 2020, 10:19:31 PM »
Thank you very much my good man!

Also I like your videos, keep up the good work!