Jump to content

source? theplayer? player? It's confusing me....


kevin433

Recommended Posts

Hi,

Yesterday I got told to use thePlayer, plr or player instead of source if I meant a Player. Now I tried to change source to player. I started the sever, joined and... nothing happens. The only thing is that I'm looking into the sky and when I see at the SeverLog, the server spams it full with

[15:22:38] WARNING: broph.lua: Bad argument @ 'spawnPlayer' - Line: 18

I changed then to plr, and that also didn't work. I tried thePlayer and that also didn't work. I then changed back to source, and everything worked fine. That's my code:

addEventHandler("onResourceStart", resourceRoot,
function()
for i,plr in ipairs(getElementsByType("player")) do
		spawn(plr)
end
end
)
 
function spawn(plr)
repeat until spawnPlayer (plr, -711+math.random(1,5), 957+math.random(5,9), 12.4, 90, math.random(9,288) )
fadeCamera(plr, true)
setCameraTarget(plr, plr)
end
 
function playerjoined()
spawn(plr)
setTimer(keys, 1000, 1, plr)
setTimer(LichtAnlageOn, 500, 0, plr)
outputChatBox("Hi Noob!", plr)
end
 
 
addEventHandler("onPlayerJoin", root, playerjoined)

Link to comment

events have the hidden variable "source" containing the element which triggered the event. thats why you were told to not overwrite it and use some other variable for player elements in functions.

you dont have to use some specific player variable, it can be anything you want, just dont overwrite "source" if it exists, cause it can be confusing later.

here you have to use that source variable, cause plr isnt defined anyway:

function playerjoined()
  spawn(source)
setTimer(keys, 1000, 1, source)
setTimer(LichtAnlageOn, 500, 0, source)
outputChatBox("Hi Noob!", source)
end
 
addEventHandler("onPlayerJoin", root, playerjoined)

you just have to understand, where to use "source" and where to pass the player element with your own variable (plr/thePlayer/theGuy/whatEverItIs), that's all.

Edited by Guest
Link to comment

You didn't understand my post then. I told you not to use source in your function parameters because events have their own source element which will be overwritten by your parameter. Some events don't pass any arguments to a function attached to the event so your source (in parameters) will be nil.

onPlayerJoin has source of the player who joined the server, so you can use that source but don't specify parameter "source" for your function... onPlayerWasted has source of player who died. onVehicleDamage has source of the vehicle which got damaged. In client-side events, it's onClientPlayerJoin, onClientPlayerWasted, etc.

You can use source in your playerjoined function because it's the player who joined the server, so:

function spawn(plr)
repeat until spawnPlayer (plr, -711+math.random(1,5), 957+math.random(5,9), 12.4, 90, math.random(9,288) )
fadeCamera(plr, true)
setCameraTarget(plr, plr)
end
 
function playerjoined()
  spawn(source) -- It's the player who joined, but there is no parameter for playerjoined as you see.
setTimer(keys, 1000, 1, plr)
setTimer(LichtAnlageOn, 500, 0, plr)
outputChatBox("Hi Noob!", plr)
end
 
 
addEventHandler("onPlayerJoin", root, playerjoined)

Link to comment
You didn't understand my post then. I told you not to use source in your function parameters because events have their own source element which will be overwritten by your parameter. Some events don't pass any arguments to a function attached to the event so your source (in parameters) will be nil.

onPlayerJoin has source of the player who joined the server, so you can use that source but don't specify parameter "source" for your function... onPlayerWasted has source of player who died. onVehicleDamage has source of the vehicle which got damaged. In client-side events, it's onClientPlayerJoin, onClientPlayerWasted, etc.

You can use source in your playerjoined function because it's the player who joined the server, so:

function spawn(plr)
repeat until spawnPlayer (plr, -711+math.random(1,5), 957+math.random(5,9), 12.4, 90, math.random(9,288) )
fadeCamera(plr, true)
setCameraTarget(plr, plr)
end
 
function playerjoined()
  spawn(source) -- It's the player who joined, but there is no parameter for playerjoined as you see.
setTimer(keys, 1000, 1, plr)
setTimer(LichtAnlageOn, 500, 0, plr)
outputChatBox("Hi Noob!", plr)
end
 
 
addEventHandler("onPlayerJoin", root, playerjoined)

If I understand it the right way, the "source" is already defined in Events like "onVehicleRespawn" "source" is the car and in events like "onPlayerClick" "source" is the player?

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