Jump to content

Starting out, and learning. Got an odd error.


Recommended Posts

Hello again. I am trying to come to grips with MTA's script handling/event handling system. Well, I made a simple script to play around with and whenever a portion of it runs, I get the following errors(Note: It works fine when joining.):

WARNING: crappylearner\script_server.lua:5: Bad argument @ 'spawnPlayer'

WARNING: crappylearner\script_server.lua:6: Bad argument @ 'setCameraTarget'

WARNING: crappylearner\script_server.lua:7: Bad argument @ 'fadeCamera''

The script:

-- Learning MTA's script system 
  
function playerSpawnAt(x, y, z) 
    outputChatBox("Calling: playerSpawnAt("..x..", "..y..", "..z..")", source) 
    spawnPlayer(source, x, y, z) 
    setCameraTarget(source, source) 
    fadeCamera(source, true) 
end 
  
function doOnJoin() 
    outputChatBox("Calling: doOnJoin()", source) 
    playerSpawnAt(2031, -1405, 17.4) 
end 
  
function doOnDeath() 
    outputChatBox("Calling: doOnDeath()", source) 
    setTimer(playerSpawnAt, 2000, 1, 2031, -1405, 17.4) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), doOnJoin) 
addEventHandler("onPlayerWasted", getRootElement(), doOnDeath) 

Any ideas? Would be awesome if someone can explain to me the why's & how's.

Link to comment

You need to pass the player element to the playerSpawnAt function, the 'source' variable doesn't exist there anymore.

-- Learning MTA's script system 
  
function playerSpawnAt(player, x, y, z) 
    outputChatBox("Calling: playerSpawnAt("..x..", "..y..", "..z..")", player) 
    spawnPlayer(player, x, y, z) 
    setCameraTarget(player, player) 
    fadeCamera(player, true) 
end 
  
function doOnJoin() 
    outputChatBox("Calling: doOnJoin()", source) 
    playerSpawnAt(source, 2031, -1405, 17.4) 
end 
  
function doOnDeath() 
    outputChatBox("Calling: doOnDeath()", source) 
    setTimer(playerSpawnAt, 2000, 1, source, 2031, -1405, 17.4) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), doOnJoin) 
addEventHandler("onPlayerWasted", getRootElement(), doOnDeath) 

Link to comment
You need to pass the player element to the playerSpawnAt function, the 'source' variable doesn't exist there anymore.
-- Learning MTA's script system 
  
function playerSpawnAt(player, x, y, z) 
    outputChatBox("Calling: playerSpawnAt("..x..", "..y..", "..z..")", player) 
    spawnPlayer(player, x, y, z) 
    fadeCamera(player, true) 
    setCameraTarget(player, player) 
end 
  
function doOnJoin() 
    outputChatBox("Calling: doOnJoin()", source) 
    playerSpawnAt(source, 2031, -1405, 17.4) 
end 
  
function doOnDeath() 
    outputChatBox("Calling: doOnDeath()", source) 
    setTimer(playerSpawnAt, 2000, 1, source, 2031, -1405, 17.4) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), doOnJoin) 
addEventHandler("onPlayerWasted", getRootElement(), doOnDeath) 

x,y,z hasnt got a spawn position so he will have another error

-- Learning MTA's script system 
  
function playerSpawnAt() 
    local x = 2031 
    local y = -1405 
    local z = 17.4 
    outputChatBox("Calling: playerSpawnAt("..x..", "..y..", "..z..")", source) 
    spawnPlayer(source, x, y, z) 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
end 
  
function doOnJoin() 
    outputChatBox("Calling: doOnJoin()", source) 
    playerSpawnAt(source, 2031, -1405, 17.4) 
end 
  
function doOnDeath() 
    outputChatBox("Calling: doOnDeath()", source) 
    setTimer(playerSpawnAt, 2000, 1, source) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), doOnJoin) 
addEventHandler("onPlayerWasted", getRootElement(), doOnDeath) 
  

change the x y z to the position you like :)

Link to comment
x,y,z hasnt got a spawn position so he will have another error

/fail - You are wrong.

-- Learning MTA's script system 
 -code- 
change the x y z to the position you like :)[/quote] 
You Are Wrong 
  
[code=lua]-- Learning MTA's script system 
  
function playerSpawnAt(player, x, y, z) 
    outputChatBox("Calling: playerSpawnAt("..getPlayerName(player)..", "..x..", "..y..", "..z..")", player) 
    spawnPlayer(player, x, y, z) 
    fadeCamera(player, true) 
    setCameraTarget(player, player) 
end 
  
function doOnJoin() 
    outputChatBox("Calling: doOnJoin()", source) 
    playerSpawnAt(source, 2031, -1405, 17.4) 
end 
  
function doOnDeath() 
    outputChatBox("Calling: doOnDeath()", source) 
    setTimer(playerSpawnAt, 2000, 1, source, 2031, -1405, 17.4) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), doOnJoin) 
addEventHandler("onPlayerWasted", getRootElement(), doOnDeath) 

That is SDK's code. Which is correct.

Link to comment

Does addCommandHandler() always pass the calling player over to the command used?

Also, where can I read on the global variables that MTA uses? Specifically source, and the others.

[edit] if source is global then why does it fail to carry over the way I had it before? And how does addEventHandler() know which player to spawn the way its set up now?

Link to comment
Does addCommandHandler() always pass the calling player over to the command used?

No, the handlerfunction has a different syntax between server- an clientside. Serverside, the first argument is the player who entered the command. Clientside commands only work for the local player (you don't know if another player has entered a clientside command), so the player element you will use is getLocalPlayer().

https://wiki.multitheftauto.com/wiki/AddCommandHandler

Also, where can I read on the global variables that MTA uses? Specifically source, and the others.

The only time hidden variables are used is when using events iirc. They are globale and exist only during the function call, and get deleted after all the code is executed.

"source" is the most used and important one, you also have some others listed here, but the only one that is important from those is the "client" variable (only when you use triggerServerEvent).

[edit] if source is global then why does it fail to carry over the way I had it before? And how does addEventHandler() know which player to spawn the way its set up now?

The reason is because you used a timer after dieing. Like I said above, source only exists during the event handler function call and gets removed afterwards. So if you die and playerSpawnAt is executed after 2 seconds, source is lost already.

The playerSpawnAt function works with onPlayerJoin since it's executed immediately, so source still exists. But I modified the doJoin part too, since I already had changed the playerSpawnAt function due to the timer.

Well, I hope I explained everything correctly ^^

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