Jump to content

Serverside variable


JeViCo

Recommended Posts

Hi everyone! I can't understand at least one thing here:

mta-screen_2018-01-25_16-50-57.jpg

When player press button "0" his weapon slot switch to fist. I almost made it. I don't know how to import slot number from client side

I made like this 

pressedkey = weap_slot

 

and then

setPedWeaponSlot(source,button) // variable button is undefined

and then i triggered client event. I have no ideas. If i trigger client, engine looks like client(setPedWeaponSlot)->server(call client slot variable)->client(returns)->server(execute)->client o.O

or i can do it in eazier way? :ph34r:

Link to comment
1 hour ago, pa3ck said:

I don't understand at least one thing here. What are you trying to achieve? client-server-client-server-client, that is not communication between server and client, that's a whole conversation.. 

just trying to switch weapons by pressing numbers on keyboard and that's it :S

Link to comment
49 minutes ago, pa3ck said:

Oh, I see, what do you have so far? Post your full code

client

function PressedKey(button, press)
    if (press) and button=="1" then
        outputChatBox("Seems to work") // detect button press
		triggerServerEvent("weap_change",localPlayer,button)
    end
end
addEventHandler("onClientKey", root, PressedKey) 

server

addEvent("weap_change",true)
addEventHandler("weap_change",root,function ()
setPedWeaponSlot(source,button) // button not found in here
end
)

I'm going to make 0-9 numbers action

Link to comment

https://wiki.multitheftauto.com/wiki/BindKey

https://wiki.multitheftauto.com/wiki/GetPedWeaponSlot

https://wiki.multitheftauto.com/wiki/SetPedWeaponSlot

This will eliminate the need for the client side file altogether, use bindKey instead and bind each key to set the players weapon slot, getPedWeaponSlot is just there too because it might be useful, depends what errors happen, I never used keys for weapons before....

You can also use all 3 of those client side, eliminating the need for server file and freeing up space on your server/host, but I do not know if that changes only for local player or for all players.

Link to comment
  • Scripting Moderators
addEvent("weap_change",true)
addEventHandler("weap_change",root,function (button)
setPedWeaponSlot(source,tonumber(button))
end
)

 

Edited by thisdp
Link to comment

thanks, now it works, but now i have 2 problems. onClientKey event makes this script work twice - when key is pressed and whet it released(with the exception of
 key "0"). Also i don't know how to make this conditions less:

	button=="0" or 
	button=="1" or
	button=="2" or 
	button=="3" or
	button=="4" or 
	button=="5" or
	button=="6" or 
	button=="7" or
	button=="8" or 
	button=="9" then

 How can i fix that all? O.o 

Edited by JeViCo
Link to comment

Right, first when you find something on the wiki, read the parameters!!!

Parameters are very useful, in your case, with the onClientKey handler the parameters are (button) and (pressed) so it returns 3 things in total...

"button" is the button pressed, "pressed" is true if the key is down, FALSE if the key is up

So instead of doing this:

function PressedKey(button, press)
    if (press) and button=="1" then

Try this:

function PressedKey(button, press)
    if (press == true) and button=="1" then

Hope this helps....  In theory, if checks if a value is true, so if (press) should only happen when your key is pressed, if this doesn't work, look into using bindKey instead and bind each different key to set the weapon slot.

Link to comment
6 hours ago, kieran said:

Right, first when you find something on the wiki, read the parameters!!!

Parameters are very useful, in your case, with the onClientKey handler the parameters are (button) and (pressed) so it returns 3 things in total...

"button" is the button pressed, "pressed" is true if the key is down, FALSE if the key is up

nope

if (press) and button=="0" or 
	(press) and button=="1" or
	(press) and button=="2" or
	(press) and button=="3" or
	(press) and button=="4" or
	(press) and button=="5" or
	(press) and button=="6" or
	(press) and button=="7" or
	(press) and button=="8" or
	(press) and button=="9" then

i tried this and it works, but it's so long. Have no ideas to make this part of code less

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