Sign in to follow this  
Protagonist95

Passing table from server side to client failed?

Recommended Posts

How can i pass serverside table to client?

--Serverside
function selectMembersList ( playerSource )
  local connection = dbConnect( "sqlite", "data.db" )
 	local qh = dbQuery( connection, "SELECT member_login FROM gang_members WHERE gang_id = ?",selectGangIDBySerial(getPlayerSerial(playerSource)))
  	local result = dbPoll ( qh, -1 )

  	local group_members = {}

	  for _, row in ipairs ( result ) do
	  	local player = getPlayerFromSerial(row["member_login"])
        	table.insert(group_members,getPlayerSerial(player))
      end
	 return group_members	--table is ok at this point
end
addEvent( "selectMembersListEvent", true )
addEventHandler( "selectMembersListEvent", resourceRoot, selectMembersList )


--Clientside

function addGroupMembers()
group_members = triggerServerEvent("selectMembersListEvent",resourceRoot,getLocalPlayer())

	for _,value in ipairs(group_members) do -- table expected,got boolean
		outputChatBox(value)
	end
end
addEventHandler( "onClientResourceStart",resourceRoot,addGroupMembers)

 

Share this post


Link to post

triggerServerEvents can't return the values they get by the function "return". You have to make an another event on clientside to trigger it from the serverside and have the table as an arg in it.

Share this post


Link to post
--Serverside
function selectMembersList ( playerSource )
  local connection = dbConnect( "sqlite", "data.db" )
 	local qh = dbQuery( connection, "SELECT member_login FROM gang_members WHERE gang_id = ?",selectGangIDBySerial(getPlayerSerial(playerSource)))
  	local result = dbPoll ( qh, -1 )

  	local group_members = {}

	  for _, row in ipairs ( result ) do
	  	local player = getPlayerFromSerial(row["member_login"])
        	table.insert(group_members,getPlayerSerial(player))
      end
      triggerClientEvent("resendValues", source, group_members)
	 return group_members	--table is ok at this point
end
addEvent( "selectMembersListEvent", true )
addEventHandler( "selectMembersListEvent", resourceRoot, selectMembersList )


--Clientside

function addGroupMembers()
	group_members = triggerServerEvent("selectMembersListEvent",resourceRoot,getLocalPlayer())
end
addEventHandler( "onClientResourceStart",resourceRoot,addGroupMembers)

function recieveValue(group_members)
	for _,value in ipairs(group_members) do -- table expected,got boolean
		outputChatBox(value)
	end
end
addEvent("resendValues", true)
addEventHandler("resendValues", resourceRoot, recieveValue)

this should work the way u want it but why don't u do something like this

-----

--Serverside
function selectMembersList()
	local connection = dbConnect("sqlite", "data.db" )
	local qh = dbQuery(connection, "SELECT member_login FROM gang_members WHERE gang_id = ?", selectGangIDBySerial(getPlayerSerial(source)))
	local result = dbPoll(qh, -1 )
	local group_members = {}
	
	for _, row in ipairs (result) do
		local player = getPlayerFromSerial(row["member_login"])
		table.insert(group_members, getPlayerSerial(player))
    end
    triggerClientEvent("resendValues", source, group_members)
end
addEvent("selectMembersListEvent", true)
addEventHandler("selectMembersListEvent", resourceRoot, selectMembersList)


--Clientside

function addGroupMembers()
	triggerServerEvent("selectMembersListEvent", getLocalPlayer())
end
addEventHandler("onClientResourceStart", resourceRoot, addGroupMembers)

function recieveValue(group_members)
	for _,value in ipairs(group_members) do -- table expected,got boolean
		outputChatBox(value)
	end
end
addEvent("resendValues", true)
addEventHandler("resendValues", resourceRoot, recieveValue)

 

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.