Jump to content

for() does not work via client-side


SrPattif

Recommended Posts

Hi.
I had this script via server-side:

-- server_side
for i,v in ipairs(resourceTable) do
	print(getResourceName(v))
end

server-side, works normally.

But in client-side, it does not work.
It just dont print nothing.

-/> i got the resources table via triggerServerEvent, and the type() of the response via client-side is really table.

if you can help, I would be grateful.

Link to comment
  • Moderators
48 minutes ago, SrPattif said:

But in client-side, it does not work.

 

That is probably because the following function is serverside only:

https://wiki.multitheftauto.com/wiki/GetResources

 

See here a workaround for clientside:

local resourceRoots = getElementsByType("resource")
for i=1, #resourceRoots do
  local thisResourceRoot = resourceRoots[i]
  local resourceName = getElementID ( thisResourceRoot ) 
  if resourceName then
    local res = getResourceFromName(resourceName)
    if res and res ~= resource and getResourceState(res)  == "running" then

    end
  end
end 

Source resource: draw-distance

https://gitlab.com/IIYAMA12/draw-distance/-/blob/master/scripts/main_c.lua#L248

 

 

 

Link to comment
33 minutes ago, IIYAMA said:

 

That is probably because the following function is serverside only:

https://wiki.multitheftauto.com/wiki/GetResources

 

See here a workaround for clientside:


local resourceRoots = getElementsByType("resource")
for i=1, #resourceRoots do
  local thisResourceRoot = resourceRoots[i]
  local resourceName = getElementID ( thisResourceRoot ) 
  if resourceName then
    local res = getResourceFromName(resourceName)
    if res and res ~= resource and getResourceState(res)  == "running" then

    end
  end
end 

Source resource: draw-distance

https://gitlab.com/IIYAMA12/draw-distance/-/blob/master/scripts/main_c.lua#L248

 

 

 

This helped me, but it only shows the scripts already initialized.
 

-- client-side

function carregarResources()
  local resourceRoots = getElementsByType("resource")
for i=1, #resourceRoots do
  local thisResourceRoot = resourceRoots[i]
  local resourceName = getElementID ( thisResourceRoot ) 
  if resourceName then
    local res = getResourceFromName(resourceName)
    if res and res ~= resource then
      guiGridListAddRow(GUI.gridlist[1], getResourceName(res))
    end
  end
end 
end


 

Link to comment
  • Moderators
49 minutes ago, SrPattif said:

This helped me, but it only shows the scripts already initialized.

That is correct, because you can't do anything with resources that are not initialized. Except maybe for the name.

(for new resources: onClientResourceStart)

 

 

If you only need the resourceNames:

You could save elementdata on the resourceRoot containing all resourceNames. Or (more secure/controllable) triggerServerEvent > triggerClientEvent to send over all the resourceNames.

But think twice about which resourceNames you send over. Since it might be a little privacy sensitive for you...

 

 

Edited by IIYAMA
Link to comment

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...