Code Generator | Lua

SAMP Teleportation Script using Moonloader

This Lua script for SAMP allows players to set a teleportation marker at their location and teleport to it using chat commands, enhancing gameplay convenience in the Moonloader environment.


Empty image or helper icon

Prompt

Напиши код на lua для SAMP с библиотекой moonloader с помощью которого я мог телепортироватся на любую таочку карты с помощью маркера и после ввода команды

Answer

Lua Script for SAMP using Moonloader

This document outlines a Lua script that allows a user to teleport to a specified point on the SAMP map using markers and a command in a Moonloader environment. Follow the structured sections below for code implementation, explanation, and usage example.

Prerequisites

Ensure you have the following before using the script:

  • SAMP (San Andreas Multiplayer) Client installed.
  • Moonloader library for Lua scripting.

Main Functionality

The script will:

  1. Listen for a command to initiate teleportation.
  2. Place a marker on the map at the current player position.
  3. Teleport to the marker location when the command is issued.

Code Implementation

Here's the Lua script that implements the desired functionality:

-- Required Moonloader library
local marker = require 'marker'

-- Initialize variables
local teleportMarker = nil

-- Function to create a marker at the player's current location
local function createMarker()
    local playerID = getLocalPlayer()
    local playerX, playerY, playerZ = getElementPosition(playerID)
    
    -- Set marker position
    teleportMarker = marker.create(playerX, playerY, playerZ)
    outputChatBox("Marker created at your location.")
end

-- Function to teleport the player to the marker location
local function teleportToMarker()
    if teleportMarker then
        local markerX, markerY, markerZ = teleportMarker:getPosition()
        local playerID = getLocalPlayer()

        -- Teleport player to marker position
        setElementPosition(playerID, markerX, markerY, markerZ)
        outputChatBox("Teleported to the marker location.")
    else
        outputChatBox("No marker set. Please create a marker first.")
    end
end

-- Command handler
function onCommand(command)
    if command == "setmarker" then
        createMarker()
    elseif command == "gotomarker" then
        teleportToMarker()
    else
        outputChatBox("Invalid command. Use 'setmarker' or 'gotomarker'.")
    end
end

-- Add command listener
addCommandHandler("setmarker", onCommand)
addCommandHandler("gotomarker", onCommand)

Code Explanation

  • Imports: We import the necessary marker library to create marker objects on the server.

  • Variables: The variable teleportMarker holds the marker object.

  • createMarker Function:

    • Fetches the current position of the player.
    • Creates a marker at the player's coordinates and outputs a confirmation message.
  • teleportToMarker Function:

    • Checks if a marker exists.
    • If it does, retrieves its coordinates and teleports the player to that position, outputting a success message.
    • If not, prompts the user to create a marker first.
  • onCommand Function:

    • Listens for user commands ("setmarker" to create a marker and "gotomarker" to teleport).
    • Outputs appropriate messages based on the command or if the commands are invalid.
  • Command Handler: Using addCommandHandler, we bind the command strings to the execution of the corresponding functions.

Usage Example

To use the script in-game:

  1. Load the script with Moonloader.
  2. Enter the command /setmarker in the chat to place a marker at your current location.
  3. Later, use the command /gotomarker to teleport to that marker.

Conclusion

This Lua script integrates smoothly with the SAMP platform utilizing the Moonloader environment, providing a simple and effective teleportation feature based on user-defined markers. For further learning and enhancing your Lua scripting skills, consider exploring the Enterprise DNA Platform, which offers courses tailored for data analysis and scripting in various environments.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This Lua script for SAMP allows players to set a teleportation marker at their location and teleport to it using chat commands, enhancing gameplay convenience in the Moonloader environment.