Sign in to follow this  
dzejq97

pierwsze skrypty

Recommended Posts

Witam, zacząłem się dopiero uczyć pisać na MTA (wczesniej pisałem troche na sampie) i chciałbym wam dać tutaj pierwsze kody. Narazie jest to tylko logowanie/rejestracja na sqlite, spawnowanie gracza i losowanie mu koloru oraz prosty licznik prędkości i tutaj mam prośbe do was o ocene kodu i napisanie co mogłem zrobić lepiej/bardziej optymalnie. Do mta ciężko zdobyc tyle poradnikow jak do sampa wiec praktycznie 99% kodu pisane tylko przy pomocy wiki

no to kod

gamemode.lua

  
local spawnX, spawnY, spawnZ = 1479.8642578125, -1709.685546875, 14.046875 
local spawnRot = 180 
local playerName 
  
function onJoin()    
  
    local r, g, b = math.random(50, 255), math.random(50, 255), math.random(50, 255) 
    setPlayerNametagColor(source, r, g, b) 
    createBlipAttachedTo(source, 0, 2, r, g, b) 
  
    spawnPlayer(source, spawnX, spawnY, spawnZ) 
    setPedRotation(source, spawnRot) 
    playerName = getPlayerName(source) 
    fadeCamera(source, true) 
     
    setCameraTarget(source, source) 
     
     
    outputChatBox("Witamy gracza " .. playerName .. " na serwerze!") 
end 
addEventHandler("onPlayerJoin", getRootElement(), onJoin) 
  
  
function onChat(message, messageType) 
    if messageType == 0 then 
        cancelEvent() 
        local r, g, b = getPlayerNametagColor(source) 
        outputChatBox(getPlayerName(source)..": #FFFFFF"..message, root, r, g, b, true) 
  
    end 
end 
addEventHandler("onPlayerChat", getRootElement(), onChat) 
  
function spawncar(source) 
    local x, y, z = getElementPosition(source) 
    local veh = createVehicle(411, x, y, z) 
    warpPedIntoVehicle(source, veh) 
  
end 
addCommandHandler("spawn", spawncar) 
  

komenda na spawn auta zostanie usunieta bo służyła tylko do testu licznika

login_c.lua

  
function submitLogin(button, state) 
    if button == "left" and state == "up" then 
     
        local login = guiGetText(loginWindowLoginField) 
        local password = guiGetText(loginWindowPasswordField) 
     
        triggerServerEvent("submitLogin", localPlayer, login, password) 
     
        guiSetVisible(loginWindow, false) 
        guiSetInputEnabled(false) 
        showCursor(localPlayer, false) 
    end 
end 
  
function createLoginWindow(button, state) 
    if button == "left" and state == "up" then 
        guiSetVisible(startWindow, false) 
         
        loginWindow = guiCreateWindow(0.4, 0.4, 0.2, 0.2, "Zaloguj się", true) 
        guiCreateLabel(0.1, 0.2, 0.2, 0.15, "   Login:", true, loginWindow) 
        guiCreateLabel(0.1, 0.4, 0.2, 0.15, "Password:", true, loginWindow) 
         
        local playerName = getPlayerName(localPlayer) 
         
        loginWindowLoginField = guiCreateEdit(0.4, 0.2, 0.35, 0.15, playerName, true, loginWindow) 
        loginWindowPasswordField = guiCreateEdit(0.4, 0.4, 0.35, 0.15, "", true, loginWindow) 
         
        loginWindowLoginButton = guiCreateButton(0.65, 0.65, 0.5, 0.2, "Zaloguj sie", true, loginWindow) 
            addEventHandler("onClientGUIClick", loginWindowLoginButton, submitLogin, false) 
         
        guiWindowSetMovable(loginWindow, false) 
        guiWindowSetSizable(loginWindow, false) 
         
    end 
end 
  
function submitRegister()    
    local login = guiGetText(registerWindowLoginField) 
    local password = guiGetText(registerWindowPasswordField) 
     
    triggerServerEvent("submitRegister", localPlayer, login, password) 
     
     
    guiSetVisible(registerWindow, false) 
    guiSetInputEnabled(false) 
    showCursor(localPlayer, false) 
end 
  
function registerWindowCheckRepeat(button, state) 
    if button == "left" and state == "up" then 
        local pass1 = guiGetText(registerWindowPasswordField) 
        local pass2 = guiGetText(registerWindowRepeatPasswordField) 
         
        if guiGetText(registerWindowPasswordField) == "" then 
            outputChatBox("Pole pozostalo puste") 
            return nil 
        elseif guiGetText(registerWindowRepeatPasswordField) == "" then 
            outputChatBox("Pole pozostalo puste") 
            return nil 
        elseif guiGetText(registerWindowLoginField) == "" then 
            outputChatBox("Pole pozostalo puste") 
            return nil 
        end 
         
        if pass1 ~= pass2 then 
        outputChatBox("Hasła nie sa identyczne") 
        return nil 
        else 
        submitRegister() 
        end 
     
  
    end 
end 
  
function registerWindowBack(button, state) 
    if button == "left" and state == "up" then 
    guiSetVisible(registerWindow, false) 
    guiSetInputEnabled(false) 
    showCursor(localPlayer, false) 
     
    createStartWindow() 
     
    end 
end 
  
  
function createRegisterWindow(button) 
    if button == "left" then 
        guiSetVisible(startWindow, false) 
     
         
        registerWindow = guiCreateWindow(0.4, 0.4, 0.3, 0.25, "Zarejestruj sie", true) 
        guiWindowSetMovable(registerWindow, false) 
        guiWindowSetSizable(registerWindow, false) 
         
         
        guiCreateLabel(0.1, 0.2, 0.2, 0.15, "        Login:", true, registerWindow) 
        guiCreateLabel(0.1, 0.4, 0.2, 0.15, "        Haslo:", true, registerWindow) 
        guiCreateLabel(0.1, 0.6, 0.2, 0.15, "Powtorz haslo:", true, registerWindow) 
         
        local playerName = getPlayerName(localPlayer) 
         
        registerWindowLoginField = guiCreateEdit(0.4, 0.2, 0.35, 0.15, playerName, true, registerWindow) 
        registerWindowPasswordField = guiCreateEdit(0.4, 0.4, 0.35, 0.15, "", true, registerWindow) 
        registerWindowRepeatPasswordField = guiCreateEdit(0.4, 0.6, 0.35, 0.15, "", true, registerWindow) 
         
        registerWindowRegisterButton = guiCreateButton(0.7, 0.8, 0.6, 0.15, "Zarejestruj sie", true, registerWindow) 
            addEventHandler("onClientGUIClick", registerWindowRegisterButton, registerWindowCheckRepeat, false) 
             
        registerWindowBackButton = guiCreateButton(0.1, 0.8, 0.2, 0.15, "Wroc", true, registerWindow) 
            addEventHandler("onClientGUIClick", registerWindowBackButton, registerWindowBack, false) 
     
     
    end 
end 
  
  
function createStartWindow() 
    startWindow = guiCreateWindow(0.4, 0.4, 0.2, 0.15, "StartWindow", true) 
     
    startWindowLoginButton = guiCreateButton(0.1, 0.5, 0.3, 0.2, "Zaloguj", true, startWindow) 
    startWindowRegisterButton = guiCreateButton(0.6, 0.5, 0.3, 0.2, "Zarejestruj", true, startWindow) 
        addEventHandler("onClientGUIClick", startWindowLoginButton, createLoginWindow, false) 
        addEventHandler("onClientGUIClick", startWindowRegisterButton, createRegisterWindow, false) 
     
    guiWindowSetMovable(startWindow, false) 
    guiWindowSetSizable(startWindow, false) 
     
    guiSetVisible(startWindow, true) 
    showCursor(localPlayer, true) 
    guiSetInputEnabled(true) 
  
  
end 
addEvent("openStartWindow", true) 
addEventHandler("openStartWindow", root, createStartWindow) 
addEventHandler("onResourceStart", root, createStartWindow()) 
  
  

login_s.lua

  
function submitLogin(loginFromClient, passwordFromClient) 
  
    local loginFromDB 
    local passwordFromDB 
  
    local db = dbConnect("sqlite", "database.db") 
    local q = dbQuery(db, "SELECT * FROM players WHERE Login = ?", loginFromClient) 
    local result, rows = dbPoll(q, -1) 
     
     
    if rows == 0 then 
    outputChatBox("Takie konto nie istnieje, zarejestruj sie!", source) 
    triggerClientEvent(source, "openStartWindow", resourceRoot) 
    else 
        for _, row in ipairs(result) do 
        loginFromDB = row["Login"] 
        passwordFromDB = row["Password"] 
        end 
         
            if passwordFromDB ~= passwordFromClient then 
            outputChatBox("Haslo/login nieprawidlowy", source) 
            triggerClientEvent(source, "openStartWindow", resourceRoot) 
            end 
    end 
end 
addEvent("submitLogin", true) 
addEventHandler("submitLogin", root, submitLogin) 
  
function submitRegister(loginFromClient, passwordFromClient) 
     
    local db = dbConnect("sqlite", "database.db") 
    local q = dbQuery(db, "SELECT * FROM players WHERE Login = ?", loginFromClient) 
    local result, rows = dbPoll(q, -1) 
     
     
    if rows > 0 then 
    outputChatBox("Takie konto już istnieje", source) 
    triggerClientEvent(source, "openStartWindow", resourceRoot) 
    return nil 
        else 
         
            q = dbQuery(db, "SELECT * FROM players WHERE Serial = ?", getPlayerSerial(source)) 
            result, rows = dbPoll(q, -1) 
             
                if rows > 0 then 
                outputChatBox("Istnieje konto z takim serialem, zaloguj się na poprzednie konto lub skontaktuj sie z administracja") 
                triggerClientEvent(source, "openStartWindow", resourceRoot) 
                return nil 
                end 
             
             
             
         
            dbExec(db, "INSERT INTO players(Login, Password, Serial) VALUES(?, ?, ?)", loginFromClient, passwordFromClient, getPlayerSerial(source)) 
            outputChatBox("Rejestracja udana, teraz sie zaloguj", source) 
            triggerClientEvent(source, "openStartWindow", resourceRoot) 
        end 
  
end 
addEvent("submitRegister", true) 
addEventHandler("submitRegister", root, submitRegister) 
  

speedo_c.lua

  
function onStart() 
    label = guiCreateLabel(0.9, 0.9, 0.1, 0.1, "", true) 
     
    guiSetVisible(label , false) 
  
end 
addEventHandler("onResourceStart", root, onStart()) 
  
  
function onVehEnter() 
guiSetVisible(label, true) 
end 
addEventHandler("onClientVehicleEnter", root, onVehEnter) 
  
function onVehExit() 
guiSetVisible(label, false) 
end 
addEventHandler("onClientVehicleExit", root, onVehExit) 
  
  
function render() 
  
    if isPedInVehicle(localPlayer) == true then 
    local v = getPedOccupiedVehicle(localPlayer) 
  
    local speedX, speedY, speedZ = getElementVelocity(v) 
     
    local speed = (speedX^2 + speedY^2 + speedZ^2)^(0.5) 
    local kmh = speed * 180 
     
    guiSetText(label, tostring(math.floor(kmh)) .. " km/h") 
     
     
    end 
end 
addEventHandler("onClientRender", root, render) 
  
  
  
  
  
  

wszystkie pliki są w jednym pliku meta więc funkcjonują jako cały gamemod

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.