[HELP/LUA] Transfer data from Client to Server to store

Hello everyone, I am working on this script that will transfer a users “vehicle” to the server using a TriggerServerEvent, and store that information into a table. Later when I execute the command /cleanup, this will list out the contents of the table and delete them. I am at lost and unsure what exactly I need to do to make the script work.

Client.lua

local pvehs = {}

Citizen.CreateThread(function()
	local players = PlayerPedId(-1)
	DecorRegister("SyncMe", -1)
	while true do
	veh = GetVehiclePedIsIn(players, false)
		Wait(0)
		if IsPedInAnyVehicle(players, false) then
			if not DecorExistOn(veh, "SyncMe") then
				DecorSetInt(veh, "SyncMe", 3)
			end
			existsInTable = false
			for k, v in ipairs(pvehs) do
				if v == veh then
					existsInTable = true
				end
				if not DoesEntityExist(v) then 
					pvehs [k] = nil
				end
			end
			if not existsInTable then
				TriggerServerEvent("test", veh)
				table.insert(pvehs, veh)
			end
		end
		print(pvehs)
	end
end)

Server.lua

decorcars = {}

RegisterCommand("cleanup", function(source)
	ace = IsPlayerAceAllowed(source, "core:cleanup")
	decorcars = {}
	if ace then
		for k, v in ipairs(decorcars) do
			DeleteEntity(v)
			print("he")
			ShowInfo("CHAR_ALL_PLAYERS_CONF", "Project X", "~g~Server cleanup has completed.")
		end
	else
		TriggerClientEvent("core:showinfo", source, "CHAR_ALL_PLAYERS_CONF", "Project X", "~r~You are not permitted this action! Contact an administrator if you believe this is an error.")
		print("he")
	end
end)

AddEventHandler("test")
RegisterServerEvent("test", function()
	print(veh)
	v = veh
	existsInTable = false
	for k, v in ipairs(decorcars) do
		if v == veh then
			existsInTable = true
		end
		if not DoesEntityExist(v) then 
			decorcars [k] = nil
		end
	end
	if not existsInTable then
		table.insert(decorcars, veh)
	end
	for k, v in ipairs(decorcars) do
		print(index)
		for v, k in pairs(decorcars) do -- DEBUGGING 
			print('\t', v, k)
		end
	end
end)

Thank you for you’re help (or looking),

Sol.

1 Like

in server.lua change line to :

RegisterServerEvent("test",function(veh)
1 Like

Good morning @yeganehha,

First, thank you for looking and assisting me! I devised new algorithms for both the client and server side, I am no longer experiencing issues with this script.

Thank you,

Moderators, may close this at their discretion.

Sol.

2 Likes