Intro to Scripting: Make an Invisibility Tool

From Goodblox Wiki
Jump to navigationJump to search
This is an Advanced, Lua related tutorial.

Introduction

This tutorial is for people who wish to learn more about creation of tools, or who wish to add an invisibility tool to their map. It contains a basic invisibility script, without fading of characters.

How the Tool Works

The invisibility tool is a HopperBin object so it will be able to be selected. It sets the Transparency values of all the player's parts to 1, which is completely transparent. To make this work in multiplayer, the script to make the player transparent has to be in the player, not the player's backpack. To this end, the true invisibility script is made to not work in the player's backpack, and is copied into the player; when the player becomes visible again, it is removed. A second script in the backpack copies the script to the player when it is activated.

Invisibility

The first script to be created is a script for hiding a model. This script can be copied into the player to make the player invisible, and can have a varied amount of time of invisibility.

Tutorial

As the script will only be two places, in a HopperBin and in a player, the script can deal with just those cases. The script should only run if it is in the player. If it is in the player, the parent of the script will have a class name of "Model". To check this, the script can have the "if" statement:

if (script.Parent.className == "Model") then

If the script is in the model, it should make it invisible. To do this, the script must find all the player's model's children. The function children() returns an array (table) of children of the object that called it. The script can assign a variable to the table produced.

modelChildList = script.Parent:children()

Next, the script must make the player's children invisible. A "for" loop can do the job.

for p = 1, #modelChildList do -- Make the player invisible
	if (modelChildList[p].className == "Part") then -- Only work on bricks
		modelChildList[p].Transparency = 1 -- Transparent
	end
end

Once the player is invisible, the script should wait for a pre-defined time. This time can either be hard-coded into the script or transmitted by the activation script. The hard-coded approach can be accomplished by:

wait(Invisibility Time)

Where Invisibility Time is the number of seconds to be invisible. The transmitted approach uses a NumberValue object. It can be implemented as:

wait(script:findFirstChild("NumberValue Name").Value)

Where NumberValue Name is a string representing the name of the number. A good name is "InvisibilityTime". After the invisibility wait time is over, the previous for-loop can be modified to make the player visible.

for p = 1, #modelChildList do -- Make the player visible
	if (modelChildList[p].className == "Part") then -- Only work on bricks
		modelChildList[p].Transparency = 0 -- Solid
	end
end

After this is done, the script is useless. It's time to get rid of it. To do this, the script's Parent value can be set to nil

script.Parent = nil

Example

If the script has been created exactly according to directions, or is just copied, then it will have the following code (with one or two changes): Note: the name of this script is "Fade"

print("Fading script loaded")
if (script.Parent.className == "Model") then -- Only deals with player characters, so no need for extra

	modelChildList = script.Parent:children()

	for p = 1, #modelChildList do -- Make the player invisible
		if (modelChildList[p].className == "Part") then
			modelChildList[p].Transparency = 1 -- Invisible
		end
	end

	wait(script:findFirstChild("InvisibilityTime").Value)

	for p = 1, #modelChildList do -- Make player visible
		if (modelChildList[p].className == "Part") then
			modelChildList[p].Transparency = 0 -- Visible
		end
	end

	script.Parent = nil -- Remove the (now useless) script from the game
end

Activation

The previous script makes a player invisible. This script activates the other script.

Tutorial

First, the script needs to know when it is selected, and with what Mouse object. This will let it know when to activate the invisibility. To this end, a function is added:

function onSelected(mouse) -- When the weapon is selected, make the script recognize clicks
	setCursor(mouse) -- Make the mouse indicate readiness. This is currently a dummy.
	mouse.Button1Down:connect(function() onButton1Down(mouse) end) -- Make the activation script be called when there is a click.
end

This will make onButton1Down() be called with an argument (input) of the mouse name whenever the mouse clicks while the tool is selected. setCursor is a dummy function (it will be added later, but it is only a stylistic thing). Next, the onButton1Down function should be added:

enabled = true -- A boolean value, made to stop multiple invisibilities
invisibilityTime = 8 -- Time the person will be invisible
reloadTime = 4 -- Additional time to reload

function onButton1Down(mouse)
	if enabled then
		enabled = false -- Stop multiple invisibilities
		setCursor(mouse) -- Not ready anymore
		playerScript = script.Parent.Fade:clone() -- Copy the fading script
		playerScript.Parent = game.Players.LocalPlayer.Character -- and place it in the player

		scriptTimeOut = Instance.new("NumberValue") -- Give it a time before becoming visible again
		scriptTimeOut.Name = "InvisibilityTime" -- This name must be coded into the other script
		scriptTimeOut.Value = invisibilityTime -- Time to stay invisible
		scriptTimeOut.Parent = playerScript

		wait(reloadTime + invisibilityTime) -- Wait for a proper reload time
		enabled = true -- Ready again
	end
	setCursor(mouse) -- Set apropriate cursor
end

setCursor is still a dummy (not made yet). This script activates the other script (named "Fade" here), and gives it a value to use as an invisibility time. With all the rest done, it's time to make setCursor a real function:

function setCursor(mouse)
	if enabled then
		mouse.Icon = "rbxasset://textures\\ArrowCursor.png"
	else
		mouse.Icon = "rbxasset://textures\\ArrowFarCursor.png"
	end
end

mouse.Icon is the image used for the cursor. "rbxasset://textures\\ArrowCursor.png" and "rbxasset://textures\\ArrowFarCursor.png" are standard cursor images available to GoodBlox clients. setCursor Changes the cursor to an appropriate image based on the enabled boolean.

Example

If the script has been made exactly according to the tutorial, or was just copied, it will look like this:

print("Invisibility hopper script loaded")

enabled = true
invisibilityTime = 8 -- Time the person will be invisible
reloadTime = 4 -- Additional time to reload

function setCursor(mouse)
	if enabled then
		mouse.Icon = "rbxasset://textures\\ArrowCursor.png"
	else
		mouse.Icon = "rbxasset://textures\\ArrowFarCursor.png"
	end
end

function onButton1Down(mouse)
	if enabled then
		enabled = false
		setCursor(mouse) -- Not ready anymore
		playerScript = script.Parent.Fade:clone() -- Copy the fading script
		playerScript.Parent = game.Players.LocalPlayer.Character -- Place it in the player
		scriptTimeOut = Instance.new("NumberValue") -- Give it a time before becoming visible again
		scriptTimeOut.Name = "InvisibilityTime"
		scriptTimeOut.Value = invisibilityTime
		scriptTimeOut.Parent = playerScript

		wait(reloadTime + invisibilityTime) -- Wait for a proper reload time
		enabled = true -- Ready again
	end
	setCursor(mouse) -- Set apropriate cursor
end

function onSelected(mouse) -- When the weapon is selected, make the script recognize clicks
	print("Invisibility selected")
	setCursor(mouse) -- Make the mouse indicate readiness
	mouse.Button1Down:connect(function() onButton1Down(mouse) end)
end

script.Parent.Selected:connect(onSelected)