Jump to content

createWeapon server side with bone_attach?


thund3rbird23

Recommended Posts

How can I make visible the created weapon to all players? I'm trying to figure out but doesn't works, I got errors in debugscript.

--- client side ---

 local x, y, z = getElementPosition(localPlayer) 
 local default = createWeapon("ak-47", x, y, z)
 local new = createObject(2965, x, y, z) 
 triggerServerEvent("newWeapon", resourceRoot, default, new)

--- server side ---

function weap(thePlayer, default, new)
   attachElements(default, new)
   exports['bone_attach']:attachElementToBone(new[thePlayer], localPlayer, 12, 0, 0, 0, 0, -90, 0)
end 
addEvent("newWeapon", true)
addEventHandler("newWeapon", resourceRoot, weap)

Debugscript errors:

clientside element 'object' at argument 4 @ 'triggerServerEvent'
clientside element 'weapon' at argument 3 @ 'triggerServerEvent'

 

Link to comment

Ok, I did it, but now if I attach weapon and my friend too in the same time and I detach the weapon then my friend can't detach... it's just keep displaying in his hand why?

 

function AKalap(thePlayer)
  x, y, z = getElementPosition(thePlayer) 
  alapak = createObject(1254, x, y, z) 
   exports['bone_attach']:attachElementToBone(alapak, thePlayer, 12, 0, 0, 0, 0, -90, 0)
end
addEvent("alapAK", true)
addEventHandler("alapAK", resourceRoot, AKalap)

function removeAKalap()
exports.bone_attach:detachElementFromBone(alapak)
moveObject(alapak, 1 ,0 ,0 ,0)
end
addEvent("removealapAK", true)
addEventHandler("removealapAK", getRootElement(), removeAKalap)

 

Link to comment
  • Moderators
3 hours ago, thund3rbird23 said:

I'm trying to figure out but doesn't works, I got errors in debugscript.

Clientside elements are not shared with the server. They simply do not exist there, only at a specific player his MTA application: MTA San Andreas 1.5\Multi Theft Auto.exe

 

How about you start at serverside tell all players in the server, that a weapon has to be attached to a specific player-element?

You already solved that.

 

 

1 minute ago, thund3rbird23 said:

the weapon then my friend can't detach... it's just keep displaying in his hand why?

Because you share the same variable for your friend his weapon as for your own.

 

10 minutes ago, thund3rbird23 said:

but now

A table is required for this kind of situation.

 

local attachedWeapons = {}

 

If you combine that with some functions, it makes it a lot easier to maintain.

function getAttachedWeaponForPlayer (player)
	return attachedWeapons[player]
end

 

function setAttachedWeaponForPlayer (player, weapon)
	attachedWeapons[player] = weapon
	return true
end

 

function destroyAttachedWeaponForPlayer (player)
	local weapon = attachedWeapons[player]
	if weapon then
		attachedWeapons[player] = nil
		if isElement(weapon) then
			return destroyElement(weapon)
		end
	end
	return false
end

 

 

 

Edited by IIYAMA
Link to comment
7 minutes ago, IIYAMA said:

How about you start at serverside tell all players in the server, that a weapon has to be attached to a specific player-element?

what do you mean? I wrote before your comment that's isn't good? Because that's attach the object but a bit buggy (?) when got attached to 2 player's hand in the same time and one of them detach then the other can't detach.

Link to comment
  • Moderators
23 minutes ago, thund3rbird23 said:

I wrote before your comment that's isn't good?

That is good. We replied at the same time, so I missed your update.

(I updated my previous post)

Edited by IIYAMA
Link to comment
36 minutes ago, IIYAMA said:

Clientside elements are not shared with the server. They simply do not exist there, only at a specific player his MTA application: MTA San Andreas 1.5\Multi Theft Auto.exe

 

How about you start at serverside tell all players in the server, that a weapon has to be attached to a specific player-element?

You already solved that.

 

 

Because you share the same variable for your friend his weapon as for your own.

 

A table is required for this kind of situation.

 


local attachedWeapons = {}

 

If you combine that with some functions, it makes it a lot easier to maintain.


function getAttachedWeaponForPlayer (player)
	return attachedWeapons[player]
end

 


function setAttachedWeaponForPlayer (player, weapon)
	attachedWeapons[player] = weapon
	return true
end

 


function destroyAttachedWeaponForPlayer (player)
	local weapon = attachedWeapons[player]
	if weapon then
		attachedWeapons[player] = nil
		if isElement(weapon) then
			return destroyElement(weapon)
		end
	end
	return false
end

 

 

 

Huh, I never used tables so this is like the Chinese language for me, lol.

Can't you substitute my code with those functions please? I just need that last deatach thing

Link to comment
  • Moderators
5 minutes ago, thund3rbird23 said:

Huh, I never used tables so this is like the Chinese language for me, lol.

Can't you substitute my code with those functions please? I just need that last deatach thing

That's why I wrapped functions around it, so that you do not even have to worry about it.

 

 

Note: for the event removealapAK you also must specify the player.

function AKalap(thePlayer)
	local x, y, z = getElementPosition(thePlayer) 
	local alapak = createObject(1254, x, y, z)
  
	setAttachedWeaponForPlayer (thePlayer, alapak) -- here
  
	exports['bone_attach']:attachElementToBone(alapak, thePlayer, 12, 0, 0, 0, 0, -90, 0)
end
addEvent("alapAK", true)
addEventHandler("alapAK", resourceRoot, AKalap)

function removeAKalap(thePlayer) -- send also the player here.
  	
	local alapak = getAttachedWeaponForPlayer (thePlayer) -- here
	if alapak then
		if isElement(alapak) then
			exports.bone_attach:detachElementFromBone(alapak)
			moveObject(alapak, 1 ,0 ,0 ,0)
		else
			destroyAttachedWeaponForPlayer (thePlayer) -- here
		end
	end
end
addEvent("removealapAK", true)
addEventHandler("removealapAK", getRootElement(), removeAKalap)

 

 

Link to comment
  • Moderators
Just now, thund3rbird23 said:

got nil for


local alapak = getAttachedWeaponForPlayer (thePlayer) -- here
outputDebugString(alapak)

 

iprint(thePlayer)

is thePlayer defined?

 

The same goes for the player from this function:

function AKalap(thePlayer)

 

 

 

 

Link to comment
9 minutes ago, IIYAMA said:

iprint(thePlayer)

is thePlayer defined?

 

The same goes for the player from this function:


function AKalap(thePlayer)


 

 

 

am I need to specify the player for event alapAK too or use localPlayer?:

 

triggerServerEvent("alapAK", resourceRoot, thePlayer)

if I do specify the player I get errors in debug.

Link to comment
  • Moderators
1 minute ago, thund3rbird23 said:

I need to specify the player for event alapAK

A specific player.

 

localPlayer is yourself as element.

So if I would join your server, then the player that I control is the localPlayer.

And the same goes for the player that you control, which is the localPlayer for you.

 

 

Link to comment
17 minutes ago, IIYAMA said:

A specific player.

 

localPlayer is yourself as element.

So if I would join your server, then the player that I control is the localPlayer.

And the same goes for the player that you control, which is the localPlayer for you.

 

 

Okay, so if  I join to the server as "Jeff" then I need to use Jeff? if so where I need to use the name Jeff? in client side where I trigger server events like this:
 


triggerServerEvent("alapAK", resourceRoot, "Jeff")

triggerServerEvent("removealapAK", resourceRoot, "Jeff")

and server side like this?:
 

function AKalap(thePlayer)
	local x, y, z = getElementPosition(thePlayer) 
	local alapak = createObject(1254, x, y, z)
  
	setAttachedWeaponForPlayer ("Jeff", alapak)
  
	exports['bone_attach']:attachElementToBone(alapak, thePlayer, 12, 0, 0, 0, 0, -90, 0)
end
addEvent("alapAK", true)
addEventHandler("alapAK", resourceRoot, AKalap)

function removeAKalap(thePlayer)
  	
	local alapak = getAttachedWeaponForPlayer ("Jeff")
	
	if alapak then
	outputDebugString(thePlayer)
	iprint(thePlayer)
		if isElement(alapak) then
			exports['bone_attach']:detachElementFromBone(alapak)
			moveObject(alapak, 1 ,0 ,0 ,0)
		else
			destroyAttachedWeaponForPlayer ("Jeff")
		end
	end
end
addEvent("removealapAK", true)
addEventHandler("removealapAK", getRootElement(), removeAKalap)

Or I'm totally lost? Sorry... it's too much for me but I really want to make works this.

 

Edited by thund3rbird23
Link to comment
  • Moderators
28 minutes ago, thund3rbird23 said:

Or I'm totally lost? Sorry... it's too much for me but I really want to make works this.

It is oke, but a lot patience is required. You need to make up your mind before resume.

 


 

First of all, you are not dealing with just 2 types of sides.

Serverside and clientside.

 

This is what you are dealing with:

17.10.jpg

 

Your servercode is only running on the server application.

 

But your clientcode is running PER player. (MTA game application)

So if there are 3 players in your server. The the same client-code will be running on each machine.

> And the important thing about this is, is that each machine is running a copy of the code.

Those copies can't communicate directly with each other and do not share data directly with each other. As you can see in the image, the server is always in between. There is no line between client 1 and client 2.

 

So:

1 serverside

3x clientside (with a copy of the code)

 

 


 

"Jeff" is not a player. This is a string value, with other words TEXT.

If you want to have access to a player called "Jeff", you can use the function getPlayerFromName. This function will search in the server for a player with this name and sends back a value which you could consider a value that helps to identify a specific player.

If the player is not in the server, the value will be false, which you could consider as NO to keep it simple.
 

 

local player = getPlayerFromName("Jeff")

if player then -- did we find a player with the name "Jeff" ?
  
end

 

if something then

do something

 

if NO then

do this not

 


 

Need a random player for testing? (serverside)

local randomPlayer = getRandomPlayer ( )

 

 

Edited by IIYAMA
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...