Jump to content

Help me please


Recommended Posts

I have created a GUI. It is supposed to either spawn you at a base (a base that I created and mapped) and/or kills you. It is supposed to be activated by a command. But it didn't work. Can you look at my script and tell me what have I done wrong? And if you know how to bind the GUI to a specific button please tell me how because I tried all methods.

function createSpawningPanel()
    SpawnWindow = guiCreateWindow(319, 196, 408, 277, "Base Spawning Panel", false)
    guiWindowSetMovable(SpawnWindow, false)
    guiWindowSetSizable(SpawnWindow, false)
    Button[1] = guiCreateButton(17, 38, 376, 73, "Go to the base", false, SpawnWindow)
    Button[2] = guiCreateButton(14, 161, 379, 71, "Kill yourself", false, SpawnWindow)
	guiSetVisible(SpawnWindow,false)
end 
addEventHandler("onClientGUIClick", Button[1], teleportPlayer, false)
addEventHandler("onClientGUIClick", Button[2], teleportPlayer, false)
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), 
function ()
    createSpawningPanel()
end
)
function openSpawnWindow()
	guiSetVisible(SpawnWindow,true)
	showCursor(true,true)
end
addCommandHandler("teleportme",openSpawnWindow)
function teleportPlayer(button,state)
    if button == "left" and state == "up" then
	if source == Button[1] then
	    triggerServerEvent("movePlayerToPosition",getLocalPlayer(),1479.6,-1612.8,14.0,0)
	elseif source == Button[2] then 
	    killPed(sourcePlayer,sourcePlayer)
	end 
	guiSetVisible(SpawnWindow,false)
	showCursor(false)
	end 
end 

 

Edited by Abbas_gamer100
Link to comment

Use code instead of quote next time, it helps readability a lot.
just a note (not an error!): getResourceRootElement(getThisResource()) = resourceRootgetThisResource() = resource (these are predefined variables)

Calling addEventHandler with Button when it's nil won't add the handler. Even the function teleportPlayer wasn't define at that point in the code.

So below there's the code without these 2 errors, try it. As always, it's untested and read quickly, so tell us if it solved the issue and if it didn't also include errors, since both of the above said problems produce errors. Use debugscript 3 when scripting an testing, otherwise check the logs (clientscript.log and server.log)
 

function createSpawningPanel()
    SpawnWindow = guiCreateWindow(319, 196, 408, 277, "Base Spawning Panel", false)
    guiWindowSetMovable(SpawnWindow, false)
    guiWindowSetSizable(SpawnWindow, false)
    guiSetVisible(SpawnWindow,false)

    Button[1] = guiCreateButton(17, 38, 376, 73, "Go to the base", false, SpawnWindow)
    addEventHandler("onClientGUIClick", Button[1], teleportPlayer, false)

    Button[2] = guiCreateButton(14, 161, 379, 71, "Kill yourself", false, SpawnWindow)
    addEventHandler("onClientGUIClick", Button[2], teleportPlayer, false)
end


addEventHandler("onClientResourceStart", resourceRoot,
    function ()
        createSpawningPanel()
    end
)

function openSpawnWindow()
    guiSetVisible( SpawnWindow, true )
    showCursor( true, true )
end
addCommandHandler( "teleportme", openSpawnWindow )

function teleportPlayer(button,state)
    if button == "left" and state == "up" then
        if source == Button[1] then
            triggerServerEvent("movePlayerToPosition",getLocalPlayer(),1479.6,-1612.8,14.0,0)
        elseif source == Button[2] then
            killPed(sourcePlayer,sourcePlayer)
        end
        guiSetVisible(SpawnWindow,false)
        showCursor(false)
    end
end

Remember that the more detailed and accurated the description of the problem is, the faster and better the fix, correction or workaround come.

Edited by LoPollo
  • Like 1
Link to comment

Another note i didn't see: getLocalPlayer() = localPlayer, just so you know.

About the problem:

17 minutes ago, Abbas_gamer100 said:

but the GUI never shows up(in the server)

In the server?

 

Here i can't see errors. Did you use debugscript 3? just to make sure there are no syntax errors...

If there are not errors then i would do this: using outputDebugString/iprint to check where the code runs and where not.
 

function createSpawningPanel()
    SpawnWindow = guiCreateWindow(319, 196, 408, 277, "Base Spawning Panel", false)
    guiWindowSetMovable(SpawnWindow, false)
    guiWindowSetSizable(SpawnWindow, false)
    guiSetVisible(SpawnWindow,false)

    Button[1] = guiCreateButton(17, 38, 376, 73, "Go to the base", false, SpawnWindow)
    addEventHandler("onClientGUIClick", Button[1], teleportPlayer, false)

    Button[2] = guiCreateButton(14, 161, 379, 71, "Kill yourself", false, SpawnWindow)
    addEventHandler("onClientGUIClick", Button[2], teleportPlayer, false)
    outputDebugString( "CreateSpawningPanel called and completed!" )
end


addEventHandler("onClientResourceStart", resourceRoot,
    function ()
        createSpawningPanel()
    end
)

function openSpawnWindow()
    guiSetVisible( SpawnWindow, true )
    showCursor( true, true )
    outputDebugString( "opened window! [i feel useless]" )
end
addCommandHandler( "teleportme", openSpawnWindow )

function teleportPlayer(button,state)
    if button == "left" and state == "up" then
        outputDebugString( "Executing teleportPlayer" )
        if source == Button[1] then
            triggerServerEvent( "movePlayerToPosition", localPlayer, 1479.6,-1612.8,14.0,0 )
            outputDebugString( "Button 1 pressed" )
        elseif source == Button[2] then
            killPed(sourcePlayer,sourcePlayer)
            outputDebugString( "Button 2 pressed" )
        end
        guiSetVisible(SpawnWindow,false)
        showCursor(false)
        outputDebugString( "teleportPlayer executed!" )
    end
end

Tell us what's shown and what not

Edited by LoPollo
Link to comment

I am pretty certain for some reason that the problem is in my meta.xml file.

Here is my meta.xml file. Maybe I didn't define the script correctly?? The script file's name is Spawn_Panel.lua

<meta>
     <info author="YourName" type="gamemode" name="My Server" description="My first MTA server" />
     <script src="script.lua" type="server"/>
	 <scipt src="Spawn_Panel.lua" type="server"/>
</meta>

 

Link to comment

It says attempt to call guiSetVisible <a nil value>...

I think I made some changed to the script and they are the reason.

Here is the script:

function createSpawningPanel()
    SpawnWindow = guiCreateWindow(319, 196, 408, 277, "Base Spawning Panel", false)
    guiWindowSetMovable(SpawnWindow, false)
	guiWindowSetSizable(SpawnWindow, false)
    guiSetVisible(SpawnWindow,false)
    Button[1] = guiCreateButton(17, 38, 376, 73, "Go to the base", false, SpawnWindow)
	addEventHandler("onClientGUIClick", Button[1], teleportPlayer, false)
    Button[2] = guiCreateButton(14, 161, 379, 71, "Kill yourself", false, SpawnWindow)
    addEventHandler("onClientGUIClick", Button[2], teleportPlayer, false)
    outputDebugString( "CreateSpawningPanel called and completed!" )
end
addEventHandler("onClientResourceStart", resourceRoot,
   function ()
       createSpawningPanel()
   end
)

function openSpawnWindow()
    guiSetVisible( SpawnWindow, true )
    showCursor( true, true )
    outputDebugString( "opened window! [i feel useless]" )
end
addCommandHandler( "teleportme", openSpawnWindow )

function teleportPlayer(button,state)
  if button == "left" and state == "up" then
     outputDebugString( "Executing teleportPlayer" )
  if source == Button[1] then
     triggerServerEvent( "movePlayerToPosition", localPlayer, 1479.6,-1612.8,14.0,0 )
     outputDebugString( "Button 1 pressed" )
     elseif source == Button[2] then
     killPed(sourcePlayer,sourcePlayer)
     outputDebugString( "Button 2 pressed" )
end
    guiSetVisible(SpawnWindow,false)
    showCursor(false)
    outputDebugString( "teleportPlayer executed!" )
end
end

 

Link to comment
13 hours ago, Abbas_gamer100 said:

YES FINALLY THANK YOU A LOT!!

The GUI has showed up. But it is bugged a lot. It only shows the "Go to the base button". And it doesn't work(not teleporting on pressing the button).

the gui is partly working, that means the file is loaded in the client, and thus the meta is correct on this

Edited by LoPollo
Link to comment

You said the gui shows up -> the meta.xml makes the script run in clientside, so the meta.xml is ok

The problem is in the script: the easiest and fastest way to solve an issue is reading the errors a script produce.
You can do this checking the log (since this script it's clientside you must check MTA San Andreas 1.5\MTA\logs\clientscript.log) or directly in-game using debugscript 3.

So my question is: what's in these files? Look for lines like this: 

[date and time] [resourceName]\Spawn_Panel.lua:[Line of the error]: [error]

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