Your first PZ LUA script

Despite endless possibilites it's almost mandatory to create a hello script as your first in a new script environment.

We'll make a slighty modified version called... Hasta la vista World!

Hello world script for Project Zomboid

And while you're working yourself up to be an vastly overpowered terminator in a world of slow moaning zombies, I'll supply you with everything you need to know.

Let's take the bad news first. We are not making manly sunglasses, but we are definitely getting an sawn off shotgun so rejoice.

So let us get right to it. We need an empty script file - not a movie one, but a LUA one. It's a plain txt file with a lua extension.

Step 1

Create empty text file in projectzomboid > media > lua > hastalavista.lua. Project zomboid will recursively look through each directory, and all .lua files will be loaded when the game starts. If you choose to put your script somewhere else, that works too as long as it is within the lua folder.

We'll start off by creating a function that will express a proper parting phrase.

function sayGoodbye() 
    getPlayer():Say("Hasta la vista zombie!");
end

Great. Now we need to decide when we be chatty. Should it be when the gamewindow starts, or when we die? NO! When we start clovering down hordes of drooling zombies of course.

Step 2

We can make our function trigger on given events. Think of events as a mailing list you sign up to. We might recieve a lot of mails from Asian businessmen, family lawyers and the Queen, but it's only the mails send by ZOMBIES that you want to react on.

In our current script we want to sign ourselves up to the attack event, which occurs everytime a player attacks.

Note that everything in LUA is very case sensititve and there is no clear letter case rules. If you scripts fail, its probably related to a letter case fault. Put this beneath our function.

Events.OnWeaponSwing.Add(sayGoodbye);

We did it! Added the sayGoodbye function to the onWeaponSwing event. So now our hero is chatty. Maybe he even over does it. Now you can actually go check it out!

Step 3

Enough with words. Zombies demands action so let us create boomstick fit for an action hero.

function shitThatsADamnShotgun(keyNum) 
    if tostring(keyNum) == tostring(Keyboard.KEY_INSERT) then
        return false; --
    end
    if getPlayer():getInventory():contains("ShotgunSawnoff") == false then
        getPlayer():getInventory():AddItem("Base.ShotgunSawnoff");
    end
    if getPlayer():getInventory():contains("ShotgunShells") == false then
        getPlayer():getInventory():AddItem("Base.ShotgunShells");
    end
end

What happens is when you push the INSERT key, the script continues. While you pushing the key, it tests if you have a shotgun in your inventory. If that's the case, you don't get another one. "aww WHY NOT?" - because they're heavy, that's why. But you get do get to have lots of shells (and they aint from the beach).

Let's add this function to the OnPlayerUpdate event

Events.OnPlayerUpdate.Add(shitThatsADamnShotgun)

The OnPlayerUpdate event triggers many times each seconds. Even as the script only runs if INSERT is pressed, you'll get a lot of shotguns. That is also why we do a check to see if you're armed.

And the result...

For your first script this is it.

function sayGoodbye()
    getPlayer():Say("Hasta la vista zombie!");
end
--Bind function to attack event
Events.OnWeaponSwing.Add(sayGoodbye);

--A function for creating a sawn off shotgun in case we didn't have one.
function shitThatsADamnShotgun(hero)
    if tostring(keyNum) == tostring(Keyboard.KEY_INSERT) then
        return false; --
    end
    if getPlayer():getInventory():contains("ShotgunSawnoff") == false then
        getPlayer():getInventory():AddItem("Base.ShotgunSawnoff");
    end
    if getPlayer():getInventory():contains("ShotgunShells") == false then
        getPlayer():getInventory():AddItem("Base.ShotgunShells");
    end
end

--Bind the function to the update timer
Events.OnKeyPressed.Add(shitThatsADamnShotgun)

Pretty simple yet pretty powerful little script. 

X

Comments

  • jbm1986

    I can't get this to work. I Put the .lua file in C:\Users\Jay\Zomboid\mods\RH_Mod\media\lua
    I filled out the mod.info file too. Loaded the game, went to mods, enabled it, etc
    got a weapon, I go to swing it annnnnd.....nothing happened...

    What do I do??

    Apr 16. 16
  • LacidOnex

    Hello! Lua seems to have a nonstandardized keyID for presses. For example, If I wanted to bind the F key, how would I incorporate that into the tostring(keyNum) == tostring(Keyboard.KEY_INSERT) string. Is there a list for all the KeyIDs?

    Jul 14. 16
  • benw

    Hello i'm ben, i'm new to game modding, thanks for the tutorial! i have 2 questions,

    1.in the following lines, what is the usage of return false?
    if tostring(keyNum) == tostring(Keyboard.KEY_INSERT) then
    return false; --
    end

    2. You mentioned "Events.OnPlayerUpdate.Add(shitThatsADamnShotgun)" but i can't see this line of code in the final script, are you mentioning to put this line of code in another file?

    Mar 31. 14
  • masoroso

    Thx, nice and easy introduction... next!

    Nov 22. 13
You need to be logged in to write comments