Skip to content

Instantly share code, notes, and snippets.

@JanHolger
Created January 2, 2020 12:27
Show Gist options
  • Save JanHolger/738bf1c813814956488cd85d63522795 to your computer and use it in GitHub Desktop.
Save JanHolger/738bf1c813814956488cd85d63522795 to your computer and use it in GitHub Desktop.
Code from my 3. Onset Scripting Tutorial
AddEvent("OnKeyPress", function(key)
if key ~= "U" then
return
end
local vehicles = GetStreamedVehicles()
local closest = nil
local dist = 100000
local pX, pY, pZ = GetPlayerLocation()
for i=1,#vehicles do
local vX, vY, vZ = GetVehicleLocation(vehicles[i])
local d = GetDistance3D(pX, pY, pZ, vX, vY, vZ)
if d < dist then
closest = vehicles[i]
dist = d
end
end
if closest == nil then
return
end
if dist > 400 then
return
end
CallRemoteEvent("LockVehicle", closest)
end)
AddEvent("OnPlayerStartEnterVehicle", function(vehicle)
local state = GetVehiclePropertyValue(vehicle, "locked")
return not state
end)
{
"author": "Youtube",
"version": "1.0",
"server_scripts": [
"server.lua"
],
"client_scripts": [
"client.lua"
],
"files": [
]
}
AddEvent("OnPlayerJoin", function(player)
SetPlayerSpawnLocation(player, 125773, 80246, 1645, 90)
AddPlayerChat(player, "Welcome, "..GetPlayerName(player).."!")
end)
print("Tutorial script has been loaded!")
AddCommand("spawnv", function(player, model, owner)
local x, y, z = GetPlayerLocation(player)
local vehicle = CreateVehicle(model, x, y, z, GetPlayerHeading(player))
SetPlayerInVehicle(player, vehicle)
SetVehiclePropertyValue(vehicle, "owner", tonumber(owner))
SetVehiclePropertyValue(vehicle, "locked", false, true)
end)
AddRemoteEvent("LockVehicle", function(player, vehicle)
local state = GetVehiclePropertyValue(vehicle, "locked")
local owner = GetVehiclePropertyValue(vehicle, "owner")
if player ~= owner then
AddPlayerChat(player, "You are not the owner of the vehicle!")
return
end
SetVehiclePropertyValue(vehicle, "locked", not state, true)
if state then
AddPlayerChat(player, "You unlocked the vehicle!")
else
AddPlayerChat(player, "You locked the vehicle!")
end
end)
local db = mariadb_connect("example.com", "tutorial", "b3rp4BQhc6J2RvLV", "tutorial")
AddEvent("OnPlayerSteamAuth", function(player)
local name = GetPlayerName(player)
local steamId = GetPlayerSteamId(player)
mariadb_query(db, "SELECT * FROM users WHERE steam_id='"..steamId.."';", function()
if mariadb_get_row_count() > 0 then
AddPlayerChatAll(name.." (#"..mariadb_get_value_name_int(1, "id")..") joined the server!")
else
mariadb_query(db, "INSERT INTO users (steam_id, name) VALUES ('"..steamId.."', '"..mariadb_escape_string(db, name).."');", function()
AddPlayerChatAll(name.." joined the server for the first time! He is player nr: "..mariadb_get_insert_id())
end)
end
end)
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment