Jump to content

[BUG] Why this doesn't work in a loop?


Recommended Posts

This code is working:

function testCmd(sourcePlayer)
	local phoneWindow = guiCreateWindow ( 0, 0, 0.5, 0.4, "Pay Phone", true )
	local tabPanel = guiCreateTabPanel ( 0, 0.1, 1, 1, true, phoneWindow )
	local mainTab = guiCreateTab( "Phone", tabPanel )
end
addCommandHandler("testcmd", testCmd)

 

 

But this code is not working:

function displayPayPhone(sourcePlayer)
	for i=1, payLength do
		if isElementInRange(sourcePlayer, payphones["PosX"..i], payphones["PosY"..i], payphones["PosZ"..i], 1) then
			local phoneWindow = guiCreateWindow ( 0, 0, 0.5, 0.4, "Pay Phone", true )
			local tabPanel = guiCreateTabPanel ( 0, 0.1, 1, 1, true, phoneWindow )
			local mainTab = guiCreateTab( "Phone", tabPanel )
		end
	end
end
addCommandHandler("payphone", displayPayPhone)

Error:

Quote

attempt to call global 'guiCreateWindow' (a nil value)

 

Edited by Dutchman101
Fix topic title for section requirements
Link to comment
4 minutes ago, andreiwow2 said:

Why would it work in the cmd and not in the loop if it was otherwise?

 

<script src="main.lua" type="shared" />

You need to know what shared means

Otherwise "guiCreateWindow" is client side function not shared or server side

Make it client

Link to comment
1 minute ago, andreiwow2 said:

Can't I just have server and client in same resource? if I use type="client" nothing works...

How it's not working

Shared functions like addCommandHandler or killPed etc....

Works on both server and client

(some of them don't have the same arguments )

Link to comment
5 minutes ago, #BrosS said:

How it's not working

Shared functions like addCommandHandler or killPed etc....

Works on both server and client

(some of them don't have the same arguments )

so now you tell me to use shared?

 

with shared, my script works, but not the gui

I get the "attempt to call global 'guiCreateWindow' error

 

with client, nothing works.

Link to comment

@andreiwow2 Just put the server and client function in a seperate file and add them to the meta. For example

 

<meta>
	<script src="server.lua" type="server"/>
	<script src="client.lua" type="client"/>
</meta>

 

In shared files, the functions must both work for the client and server, in the mta functions, the server and clientside functions differ from each other, also your code might contain clientside or server code within the callback functions. It won't work like that. Eventhough it says shared on the wiki, just put them in the appropriate file, for whichever side you're working on.

Edited by Tails
Link to comment
2 minutes ago, andreiwow2 said:

so now you tell me to use shared?

 

with shared, my script works, but not the gui

I get the "attempt to call global 'guiCreateWindow' error

 

with client, nothing works.

use client

Tell me what debug says

Link to comment
Just now, #BrosS said:

use client

Tell me what debug says

Can  you tell me where to find the debug, because with client nothing works and nothing is displayed in console

1 minute ago, Tails said:

@andreiwow2 Just put the server and client function in a seperate file and add them to the meta. For example

 


<meta>
	<script src="server.lua" type="server"/>
	<script src="client.lua" type="client"/>
</meta>

 

This also works, but I still get the "attempt to call global 'guiCreateWindow'" error

Link to comment
3 minutes ago, andreiwow2 said:

Can  you tell me where to find the debug, because with client nothing works and nothing is displayed in console

Use your first code that working

Remove the command

and then add this

addCommandHandler("payphone",
  function()
for i=1, payLength do
		if isElementInRange(localPlayer, payphones["PosX"..i], payphones["PosY"..i], payphones["PosZ"..i], 1) then
      guiSetVisible(win,true)
      end
    end
  end
  )

Change wind to your window i couldnt get it cuz i'm on mobile

Link to comment
Just now, Tails said:

Read my post again, I edited it.

Oh wait, so you say to have the client in a different lua file?

Woah... and how do I use the variables from a file to another one?

 

Like if I display the window in the client.lua, how do I pass information to the server file to use it later, for example, if the player calls someone

Link to comment

You'll have to use triggers for that, or element data but I don't recommend that.

Read here: https://wiki.multitheftauto.com/wiki/TriggerServerEvent

For example, server-side:

addEvent("getData", true)

local someData = {
	{name = "alice", age = 20},
	{name = "jack", age = 21},
	{name = "jill", age = 18},
	{name = "jones", age= 19},
}

function getPlayerDataByName(name)
	for i=1, #someData do
		if someData[i].name == name then
			return someData[i]
		end
	end	
end

addEventHandler("getData", resourceRoot,
	function(name)
		local data = getPlayerDataByName(name)
		triggerClientEvent("onGetServerData", resourceRoot, data)
	end
)

And client-side:

addEvent("onGetServerData", true)

function getPlayerData(name)
	triggerServerEvent("getData", resourceRoot, name)
end

getPlayerData("alice")


addEventHandler("onGetServerData", resourceRoot,
	function(data)
		iprint(data)
	end
)

 

Something like that, untested :P

Edited by Tails
Link to comment
21 minutes ago, pa3ck said:

From server to client use triggerClientEvent

From client to server use triggerServerEvent

 

20 minutes ago, Tails said:

You'll have to use triggers for that, or element data but I don't recommend that.

Read here: https://wiki.multitheftauto.com/wiki/TriggerServerEvent

For example, server-side:


addEvent("getData", true)

local someData = {
	{name = "alice", age = 20},
	{name = "jack", age = 21},
	{name = "jill", age = 18},
	{name = "jones", age= 19},
}

function getPlayerDataByName(name)
	for i=1, #someData do
		if someData.name == name then
			return someData[i]
		end
	end	
end

addEventHandler("getData", resourceRoot,
	function(name)
		local data = getPlayerDataByName(name)
		triggerClientEvent("onGetServerData", resourceRoot, data)
	end
)

And client-side:


addEvent("onGetServerData", true)

function getPlayerData(name)
	triggerServerEvent("getData", resourceRoot, name)
end

getPlayerData("alice")


addEventHandler("onGetServerData", resourceRoot,
	function(data)
		iprint(data)
	end
)

 

Hey guys, I've done this:

 

<meta>
<info author="Andrei" description="Pay phone script" version="0.7" name="PayPhones"/>
<script src="main.lua" type="server" />
<script src="client.lua" type="client" />
<min_mta_version server="1.1.1-9.03328" />
</meta>


Server file:

addCommandHandler("payphone",
  function(sourcePlayer)
	for i=1, payLength do
		if isElementInRange(sourcePlayer, payphones["PosX"..i], payphones["PosY"..i], payphones["PosZ"..i], 1) then
			triggerClientEvent(sourcePlayer, "onPayPhone", sourcePlayer)
		end
	end
  end
)


Client file:

addEvent("onPayPhone", true)
function displayPayPhone(sourcePlayer)
	phoneWindow = guiCreateWindow ( 0, 0, 0.5, 0.4, "Pay Phone", true )
end
addEventHandler("onPayPhone", sourcePlayer, onPayPhone)

 

Nothing happens when I type /payphone

Link to comment
  • Moderators
triggerClientEvent(sourcePlayer, "onPayPhone", resourceRoot, sourcePlayer)

< server

(3e argument is the source of the event that will be executed. The source isn't a parameter.)

The source is now the resourceRoot.

-------

Client >

addEventHandler("onPayPhone", resourceRoot, onPayPhone)

 

The resourceRoot element is shared over client and serverside. This eventhandler will only accept the resourceRoot of this resource.

Link to comment
2 hours ago, IIYAMA said:

triggerClientEvent(sourcePlayer, "onPayPhone", resourceRoot, sourcePlayer)

< server

(3e argument is the source of the event that will be executed. The source isn't a parameter.)

The source is now the resourceRoot.

-------

Client >


addEventHandler("onPayPhone", resourceRoot, onPayPhone)

 

The resourceRoot element is shared over client and serverside. This eventhandler will only accept the resourceRoot of this resource.

And can you show me the working example of what I've been trying to do please? What should I modify at my code, sorry but I understand better from examples.

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