Lua Scripting Tutorial - 4

Roblox Lua Scripting Tutorial - Episode 4: User Input and Advanced Functions

by ReauofinveFb

Author Avatar

Roblox Lua Scripting Tutorial - Episode 4: User Input and Advanced Functions

Welcome to the fourth episode of our Roblox Lua scripting tutorial series! In this episode, we'll delve into handling user input, working with functions that accept parameters, and explore more advanced scripting techniques.

Prerequisites

Before we begin, ensure you've completed Episode 3 and have a basic understanding of loops, conditional statements, and events in Roblox scripting.

User Input

Handling user input is crucial for creating interactive games. Let's explore how to detect keyboard and mouse input:

local part = script.Parent
local clickCount = 0

local function handleClick()
    clickCount = clickCount + 1
    print("Part clicked " .. clickCount .. " times.")
    
    if clickCount >= 5 then
        part.BrickColor = BrickColor.new("Bright red")
        
        -- Reset clickCount
        clickCount = 0
    end
end

part.Touched:Connect(handleClick)

local function onKeyPress(input)
    if input.KeyCode == Enum.KeyCode.Space then
        print("Spacebar pressed!")
    end
end

game:GetService("UserInputService").InputBegan:Connect(onKeyPress)

In this script, we've added a new function onKeyPress that listens for the Spacebar key press. When the Spacebar is pressed, it prints a message to the output console.

Functions with Parameters

Functions can accept parameters, allowing you to pass data to them. Let's create a function that changes the color of the Part:

local part = script.Parent
local clickCount = 0

local function handleClick()
    clickCount = clickCount + 1
    print("Part clicked " .. clickCount .. " times.")
    
    if clickCount >= 5 then
        changeColor(part, "Bright red")
        clickCount = 0
    end
end

part.Touched:Connect(handleClick)

function changeColor(object, newColor)
    object.BrickColor = BrickColor.new(newColor)
end

We've created a new function changeColor that accepts two parameters: the object whose color we want to change and the new color.

Advanced Scripting Techniques

Here are some additional scripting techniques you can explore:

Raycasting

Raycasting allows you to detect objects in the game world based on a ray's direction. You can use this for things like shooting mechanics:

local function onMouseClick()
    local mouse = game.Players.LocalPlayer:GetMouse()
    local ray = Ray.new(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100)
    local hit = game.Workspace:FindPartOnRay(ray)
    
    if hit and hit.Parent then
        print("Hit something: " .. hit.Parent.Name)
    end
end

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        onMouseClick()
    end
end)

Tweening

Tweening allows you to smoothly animate properties over time. You can use this for creating smooth transitions:

local part = script.Parent
local targetSize = Vector3.new(10, 10, 10)
local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear)

local function startTween()
    local tween = game.TweenService:Create(part, tweenInfo, {Size = targetSize})
    tween:Play()
end

part.Touched:Connect(startTween)

In this example, when the Part is touched, it smoothly grows to a size of (10, 10, 10) over 2 seconds.

Conclusion

In this episode, you've learned how to handle user input, work with functions that accept parameters, and explored some advanced scripting techniques in Roblox Lua. These techniques will help you create more interactive and engaging games.

Stay tuned for the next episode, where we'll explore more advanced topics like scripting GUIs and using external libraries. Happy scripting!


Tutorial created by ReauofinveFb

View in-game to comment, award, and more!