Jump to content

trigger clientside event error


hipolitalakaj

Recommended Posts

Hi!

I trying to trigger a clientside event in server.lua which exists in client.lua but the debugscript says "Server triggered clientside event onClientReceiveItems, but event is not added clientside"

What's wrong? 

---- client ----

function receiveItems(data)
	item_table = {}
	item_table = data

	if(itemElement and getElementType(itemElement) == "player" and itemElement == localPlayer or itemElement==nil )then
		item_table_player = data
		for index, value in ipairs (item_table) do
			if value[5] > 0 then
				item_table_action[tonumber(value[5])] = {item_table[tonumber(index)][6], item_table[tonumber(index)][1], item_table[tonumber(index)][3]}
			end
		end
	end

	isMove = false
	movedItem = 0
	newCount = 0
	movedSlot = -1
	movedCount = 0
	movedValue = 0
	selectedAmount = 0
	currentActionSlot = 0
end
addEvent("onClientReceiveItems", true)
addEventHandler("onClientReceiveItems", getRootElement(), receiveItems)

---- server ----

function loadPlayerItems(element)
	local ownerID = tonumber(getElementData(element, "acc:id") or -1) 

	array[0][ownerID] = {}

	local item = -1
		
	for i = 1, 50 do
		array[0][ownerID][i] = {-1, -1, -1, -1, -1, -1, -1}
	end

	local loadItems = dbPoll(dbQuery(connection, "SELECT * FROM items WHERE owner = ? AND type = 0", ownerID), -1)
	if #loadItems > 0 then
		for i, row in ipairs(loadItems) do
			item = tonumber(row['itemid']) or -1
			value = row['value'] or -1
			count = tonumber(row['count']) or -1
			slot = tonumber(row['slot']) or -1
			type = tonumber(row['type']) or -1
			slot = tonumber(row['slot']) or -1
			id = tonumber(row['id']) or -1
			actionslot =  tonumber(row['actionslot']) or -1
			duty = tonumber(row['dutyitem']) or -1
			array[tonumber(getType(element)) or 0][ownerID][slot] = {tonumber(item), value, tonumber(count), tonumber(id), tonumber(actionslot), tonumber(slot), tonumber(duty)}
		end
	end

	triggerClientEvent(element, "onClientReceiveItems", element, array[tonumber(getType(element)) or 0][ownerID]) 
end
addEvent("playerLoadItemsToServer", true)
addEventHandler("playerLoadItemsToServer", root, loadPlayerItems)

 

Link to comment
  • Moderators

See this topic:

That guy has the same issue.

 

You get this error because you send a trigger event, but the clients/players haven't loaded their scripts.
This can be either an timing issue or there is another error which causes the script to fail loading.

The event onClientResourceStart is a great way to detect when a player can receive triggerClientEvents

 

 

 

Edited by IIYAMA
Link to comment

Use onClientResourceStart to trigger the event "playerLoadItemsToServer" or trigger another server event to mark that the client is ready to communicate with the server side.

Also, you'll need to remove any call for the function 'loadPlayerItems'  in events that should be causing that error (it may be the event "onResourceStart" for example).

Edited by DNL291
Link to comment

What do you mean? Something like this?

---- client ----

function receiveItems(data)
	item_table = {}
	item_table = data

	if(itemElement and getElementType(itemElement) == "player" and itemElement == localPlayer or itemElement==nil )then
		item_table_player = data
		for index, value in ipairs (item_table) do
			if value[5] > 0 then
				item_table_action[tonumber(value[5])] = {item_table[tonumber(index)][6], item_table[tonumber(index)][1], item_table[tonumber(index)][3]}
			end
		end
	end

	isMove = false
	movedItem = 0
	newCount = 0
	movedSlot = -1
	movedCount = 0
	movedValue = 0
	selectedAmount = 0
	currentActionSlot = 0
end
addEventHandler("onClientResourceStart", getRootElement(), receiveItems)

 

Link to comment

Nope, actually in that code the table "data" isn't being passed to the function.

What I mean is, where are you calling the function 'loadPlayerItems'? You should first fix what is causing this error message. After that, follow this step I said above:

On 21/01/2019 at 00:30, DNL291 said:

Use onClientResourceStart to trigger the event "playerLoadItemsToServer" or trigger another server event to mark that the client is ready to communicate with the server side.

 

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...