Skip to content

Script Generator

Overview

The Script Generator is a window in the Core Editor that has a number of sample Scripts that can be used for common patterns. It is a great tool for scripting beginners to understand how to implement basic functionality. It is also helpful for advance users to easily add code that is frequently used. There are different types of scripts that can be generated and most of them use the Event Log to print default messages for testing.

Script Generator Window

The Window Location

There are two main ways to open the Script Generator window. The first method is to click the Script button Script and select Open Script Generator. The second method is to open the Window dropdown and select Script Generator.

The Window Layout

The Script Generator window has four main sections.

  1. The dropdown menu for the type of script.
  2. Options to change the script.
  3. Three buttons to perform different actions on the script.
  4. A preview of the script.

Some types of script do not have options to change the script.

Layout

Script Generator Types

The Script Generator has seven different types of scripts available. Some script types have suggested parent objects for the generated script to work properly.

Script Generator Type Description Suggested Parent
Ability Creates functions that connect to the start of each phase for an Ability. Also has a function connecting to the Tick event of the Ability. The Ability object.
AI Activity Creates a table of functions that defines a new AIActivity to be managed by an AIActivityHandler. The AIActivityHandler object.
Animation Names Three examples for setting an Ability's Animation, setting a player's Animation Stance, and attaching an object to a player's Socket. Depends on the example.
Event: Binding Pressed/Released Creates two functions that connect to the events of when an Input Binding is pressed and released. Server context.
Event: Button Clicked/Hovered/Unhovered Creates three functions that connect to the events of a UI Button being clicked, hovered, and unhovered by the mouse. The UIButton object.
Event: Player Join/Leave Creates two functions that connect to the events of a Player joining and leaving the game. Server context.
Event: Trigger Overlap Creates three functions that connect to the events of a Trigger beginning an overlap, ending an overlap, and being interacted. The Trigger object.

Script Generator Options

Depending on the type of script, there may be options available to change the script to a creator's preference. One example is the Ability script type has options to include or exclude functions that connect to certain ability events. Another example is the Animation Names script type has a dropdown menu to choose an animation stance for the player.

The Animation option list is not complete, but the Animations API has all the animation names.

Options

Script Generator Buttons

There are three buttons above the script preview.

  1. Regenerate Output button resets the script to its original state. This is useful if the preview code was changed and needs to be reset.
  2. Copy to Clipboard button copies the entire script to the clipboard. The script can be pasted using Ctrl+W.
  3. Create New Script button creates a new script with all the preview code inside of it.

Buttons

Creating a Dance Trigger

This example will be using the Script Generator to make a script that uses a Trigger to change a player's animation stance. The player starts dancing when it enters the trigger and stops dancing when it exits the trigger.

Add a Trigger

Open the Core Editor to a new blank project. Add a Trigger object by pressing the keyboard shortcut 9. Move the trigger to be in front of the Spawn Point of the player.

Use the keyboard shortcut V to display Gizmos if the Trigger and Spawn Point are not visible.

Trigger

Create the DanceTrigger Script

Open the Window dropdown menu and select Script Generator to open the Script Generator window. Set the Script Type to Event: Trigger Overlap. Then click the Create New Script button and name the script DanceTrigger.

Trigger Generator

Parent the DanceTrigger Script

Open the Project Content window and search for the DanceTrigger script. Drag and drop the script into the Trigger object inside the Hierarchy window.

The script needs to be a child of the Trigger because the code tries to reference the Trigger as the parent.

Trigger Parent

Test the Trigger

The generated DanceTrigger script has code that will print messages to the Event Log when a player begins and ends an overlap with the trigger. Open the Event Log window and preview the project. Then notice if any messages are printed when the player overlaps the trigger.

Copy the Animation Names Script

Open the Script Generator window and select the Animation Names script type. Set the Animation Stance option to unarmed_dance_basic. Select the line of code that changes the player's animation stance in the Script Preview section and copy it using the keyboard shortcut Ctrl+C.

player.animationStance = "unarmed_dance_basic"

Animation

Paste the Copied Script

In the Hierarchy, right click the DanceTrigger script and select Edit Script to open the Script Editor. Underneath the print statement inside the OnBeginOverlap function, paste the copied code. In the pasted code, change the player variable to be named other.

local trigger = script.parent

function OnBeginOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": Begin Trigger Overlap with " .. other.name)
        other.animationStance = "unarmed_dance_basic"
    end
end
DanceTrigger
local trigger = script.parent

function OnBeginOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": Begin Trigger Overlap with " .. other.name)
        other.animationStance = "unarmed_dance_basic"
    end
end

function OnEndOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": End Trigger Overlap with " .. other.name)
    end
end

function OnInteracted(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": Trigger Interacted " .. other.name)
    end
end

trigger.beginOverlapEvent:Connect(OnBeginOverlap)
trigger.endOverlapEvent:Connect(OnEndOverlap)
trigger.interactedEvent:Connect(OnInteracted)

The player variable needs to be changed because in the OnBeginOverlap function there is a parameter named other that is referencing the Player overlapping the Trigger.

Copy the Animation Names Script

Open the Script Generator window and select the Animation Names script type. Set the Animation Stance option to unarmed_stance. Select the line of code that changes the player's animation stance in the Script Preview section and copy it using the keyboard shortcut Ctrl+C.

player.animationStance = "unarmed_stance"

Regular Animation

Paste the Copied Script

In the Hierarchy, right click the DanceTrigger script and select Edit Script to open the Script Editor. Underneath the print statement inside the OnEndOverlap function, paste the copied code. In the pasted code, change the player variable to be named other.

function OnEndOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": End Trigger Overlap with " .. other.name)
        other.animationStance = "unarmed_stance"
    end
end
DanceTrigger
local trigger = script.parent

function OnBeginOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": Begin Trigger Overlap with " .. other.name)
        other.animationStance = "unarmed_dance_basic"
    end
end

function OnEndOverlap(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": End Trigger Overlap with " .. other.name)
        other.animationStance = "unarmed_stance"
    end
end

function OnInteracted(whichTrigger, other)
    if other:IsA("Player") then
        print(whichTrigger.name .. ": Trigger Interacted " .. other.name)
    end
end

trigger.beginOverlapEvent:Connect(OnBeginOverlap)
trigger.endOverlapEvent:Connect(OnEndOverlap)
trigger.interactedEvent:Connect(OnInteracted)

Test the Project

Save the DanceTrigger script and preview the project. The player's animation stance should now change when entering and exiting the trigger.

Learn More

Getting Started | Triggers | Animations API | Intro to Scripting | Lua Scripting Tutorial | Advanced Scripting in Core


Last update: January 6, 2022