thund3rbird23 Posted September 11, 2019 Share Posted September 11, 2019 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
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 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 IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 (edited) 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 September 11, 2019 by IIYAMA Link to comment
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 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 IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 (edited) 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 September 11, 2019 by IIYAMA Link to comment
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 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 IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 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
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 20 minutes ago, IIYAMA said: Note: for the event removealapAK you also must specify the player. Do you mean that?: triggerServerEvent("removealapAK", resourceRoot, thePlayer) Anyways don't works or I doing something wrong... it's get attached but can't detach and no error in debugscript. Link to comment
Moderators IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 3 minutes ago, thund3rbird23 said: Anyways don't works or I doing something wrong... it's get attached but can't detach and no error in debugscript. And what do you have to do when there is no error and still figure out what is going on? Answer can be found here if you do not know what to do Link to comment
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 7 minutes ago, IIYAMA said: 12 minutes ago, thund3rbird23 said: And what do you have to do when there is no error and still figure out what is going on? got nil for local alapak = getAttachedWeaponForPlayer (thePlayer) -- here outputDebugString(alapak) Link to comment
Moderators IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 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
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 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 IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 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
thund3rbird23 Posted September 11, 2019 Author Share Posted September 11, 2019 (edited) 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 September 11, 2019 by thund3rbird23 Link to comment
Moderators IIYAMA Posted September 11, 2019 Moderators Share Posted September 11, 2019 (edited) 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: 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 September 11, 2019 by IIYAMA Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now