Jump to content

Recommended Posts

I'm daring myself to add more little things to the tutorials shown here. It's of great pleasure to announce that the following code works. ☺️
I know it is not much, but it's one more step closer to my dream server.

So,

function joinHandler()
	spawnPlayer(source, 2023, 1008, 10.83, -90, math.random (1,288))
	fadeCamera(source, true)
	setCameraTarget(source, source)
	outputChatBox("¡Bienvenido!", source)
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)

You can skip the spanish words.

The result is as expected: when the player joins, the chatbox shows the text, the player is looking at the east, and the skin is randomized between 1 and 288. However, the camera is aiming at its default value: 0. The result has a rather inconvenient detail: the camera is looking at the right side of the character's body.

UPDATE: I added setPedCameraRotation. There are no errors being detected, but the camera is not changing at all.

function joinHandler()
	spawnPlayer(source, 2023, 1008, 10.83, -90, math.random (1,288))
	fadeCamera(source, true)
	setCameraTarget(source, source)
	setPedCameraRotation(source, 90)
	outputChatBox("¡Bienvenido!", source)
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)

How can I modify where the camera is looking at when a player joins, so that way it looks at the same place the character is looking?

Edited by Moony
Further research.
Link to comment

Using setPedCameraRotation on a player will not work, it is meant for peds only... You can point the camera at a X, Y, Z position using the same setCameraTarget function instead of a Element Target, but that must be done Client Side. Therefore, you could call a Client Event and then set the Camera's Target using the X,Y,Z Method

Link to comment
15 hours ago, ReZurrecti0n said:

Using setPedCameraRotation on a player will not work, it is meant for peds only... You can point the camera at a X, Y, Z position using the same setCameraTarget function instead of a Element Target, but that must be done Client Side. Therefore, you could call a Client Event and then set the Camera's Target using the X,Y,Z Method

I'm new to this. I'm having trouble understanding the functional part of it. I do understand the idea, though.

This is what I understood from your comment: since this is a client event, I need to create a script that's running in a "client.Lua" file. I created it and edited the meta.xml to look for the source file (type="client"). Up to this point, is this the correct way to proceed?

The script looks like this:

function joinHandler()
  	-- player spawns at coordinates 2023, 1008, 10.83,  with a rotation of -90, and a randomized skin id between 1 and 288.
	spawnPlayer(source, 2023, 1008, 10.83, -90, math.random (1,288))
  	-- screen fades from black to color.
	fadeCamera(source, true)
	-- if target not defined, syntax maintains player targeted
	setCameraTarget(90, 90, 90) -- (targetX, targetY, targetZ) adding different values ranging 0-359 changes nothing
end
addEventHandler("onPlayerJoin", getRootElement(), joinHandler)

I added X, Y, Z = 90 to have round values and to see a noticable change to the camera, if any. And just to be sure, I added the same line to server.Lua. Console threw an error coming from server.Lua, so I deleted the line and the error went away. However, the script doesn't seem to be working. Nothing is happening. The camera is still pointing to the north. There are no errors coming from client.Lua.

(off context: how come the error only comes from server.Lua, even though both server.Lua and client.Lua have the same line?)

Edited by Moony
Link to comment

Yes, let's say you have 2 script files named Server.Lua and Client.Lua. You know what file is which, but we must also tell the server which file is which and that is done in the meta file.

<meta>
    <script src="Server.Lua" type="server" />
    <script src="Client.Lua" type="client" />
</meta>

Alright, so now then, we want to call something to run from the Client File, so we will make some function in the Client File (AKA Client Side):

function ClientTest() -- The custom function name
  outputChatBox("Test: I'm the Client Script!") -- Just a very basic test message
end

addEvent("Client-ClientTest",true) -- We must add this event to be able to call it from the Server Script
addEventHandler("Client-ClientTest",getRootElement(),ClientTest) -- We must also add this Event Handler to point it correctly at our Function

Now we'll be able to call the new function from the Server File (AKA Server Side):

function SomeFunction()
  outputChatbox("This is the Server Script!",source) -- Another very basic message
  triggerClientEvent(source,"Client-ClientTest",source) -- Calls the Client Side Event/Function
end

In this example, you would get 2 messages in the chat, one from the Server and the other from the Client

If you use setCameraTarget(90, 90, 90) then it will show where ever X(90) is, Y(90) is and Z(90) is and thats not what you want... What you need to do is figure out where you want the camera to be looking at and use the coordinates of that exact location (No angles, rotations, etc)

Edited by ReZurrecti0n
Link to comment
On 19/02/2020 at 18:13, ReZurrecti0n said:

Yes, let's say you have 2 script files named Server.Lua and Client.Lua. You know what file is which, but we must also tell the server which file is which and that is done in the meta file.

Spoiler


<meta>
    <script src="Server.Lua" type="server" />
    <script src="Client.Lua" type="client" />
</meta>

 

Alright, so now then, we want to call something to run from the Client File, so we will make some function in the Client File (AKA Client Side):

Spoiler


function ClientTest() -- The custom function name
  outputChatBox("Test: I'm the Client Script!") -- Just a very basic test message
end

addEvent("Client-ClientTest",true) -- We must add this event to be able to call it from the Server Script
addEventHandler("Client-ClientTest",getRootElement(),ClientTest) -- We must also add this Event Handler to point it correctly at our Function

 

Now we'll be able to call the new function from the Server File (AKA Server Side):

Spoiler


function SomeFunction()
  outputChatbox("This is the Server Script!",source) -- Another very basic message
  triggerClientEvent(source,"Client-ClientTest",source) -- Calls the Client Side Event/Function
end

UPDATE: outputChatbox changed to outputChatBox.

In this example, you would get 2 messages in the chat, one from the Server and the other from the Client

 

Is it possible that there's a server.Lua 'EventHandler' missing? Nothing's happening when I start the resource from the admin panel. I tried adding an 'addCommandHandler', but the debugscript is giving me an error on that error.

I left the other files untouched. My server.Lua looks like this:

Spoiler

function SomeFunction()
  outputChatbox("This is the Server Script!",source) -- Another very basic message
  triggerClientEvent(source,"Client-ClientTest",source) -- Calls the Client Side Event/Function
end

addCommandHandler ("test", someFunction)

UPDATE: added 'addCommandHandler'.

 

On 19/02/2020 at 18:13, ReZurrecti0n said:

If you use setCameraTarget(90, 90, 90) then it will show where ever X(90) is, Y(90) is and Z(90) is and thats not what you want... What you need to do is figure out where you want the camera to be looking at and use the coordinates of that exact location (No angles, rotations, etc).

I'm sorry, but I don't exactly understand how to relate the setCameraTarget with the server/client relationship.

Edited by Moony
Some errors.
Link to comment
addCommandHandler ("test", someFunction) -- Does not match up to the function, case sensitive, it should be: SomeFunction

Anyway, my SomeFunction was just an example. If you copy and paste it only, then nothing will ever trigger SomeFunction(), therefore nothing would happen... It was meant for you to look at the example and then use it in your own code at the point you need it. But, just for understanding purposes, if you fix the command handler, then the command will trigger it and will then give you a better idea of how to implent/mod/use it into your own code

Look over the Wiki Page for setCameraTarget and you may notice there are 2 ways of using this function IF used clientside: https://wiki.multitheftauto.com/wiki/SetCameraTarget

Link to comment
4 hours ago, ReZurrecti0n said:

addCommandHandler ("test", someFunction) -- Does not match up to the function, case sensitive, it should be: SomeFunction

 

I'm struggling to make it work.
I know it's just to name an example. I just felt curious and decided to experiment with it. After hours of trial and error, I couldn't find the issue. It's not working.

The script looks like this (server.Lua):

function ShowExample()
	outputChatBox("This is the Server Script!") -- Another very basic message
  -- triggerClientEvent (source, "Client-ClientTest", source) -- Momentarily disabled.
end
addCommandHandler ("test", ShowExample)

Nothing is happening. No errors, no sentences in chatbox...

May I ask one last thing (even though you've already helped me dozens of times over other posts xD)? Would you take a look at it and see if there is anything I'm missing?

Edited by Moony
Typos.
Link to comment
  • Scripting Moderators
3 hours ago, Moony said:

I'm struggling to make it work.
I know it's just to name an example. I just felt curious and decided to experiment with it. After hours of trial and error, I couldn't find the issue. It's not working.

The script looks like this (server.Lua):


function ShowExample()
	outputChatBox("This is the Server Script!") -- Another very basic message
  -- triggerClientEvent (source, "Client-ClientTest", source) -- Momentarily disabled.
end
addCommandHandler ("test", ShowExample)

Nothing is happening. No errors, no sentences in chatbox...

May I ask one last thing (even though you've already helped me dozens of times over other posts xD)? Would you take a look at it and see if there is anything I'm missing?

It is because you cannot attach your handler to commands like: check ; list ; test

Check wiki for more info - https://wiki.multitheftauto.com/wiki/AddCommandHandler

Good luck on your adventure with Lua in MTA:SA :)

Edited by majqq
  • Thanks 2
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...