iMonkr Posted May 13, 2021 Share Posted May 13, 2021 (edited) testdoor = createObject(1499, 1580.03149, -1631.73987, 13.38281) bindKey(source, "e", "down", function() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) else setElementFrozen ( testdoor, currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) end end) its showing an error bad argument @ 'bindkey' [expected player argument 1, but nil] Edited May 13, 2021 by iMonkr Link to comment
Other Languages Moderators androksi Posted May 13, 2021 Other Languages Moderators Share Posted May 13, 2021 (edited) Hello and welcome to the forums. source, in this case, is not an element. You will need to bind the key when the resource starts and also when a player joins the server. If it's client-side, just remove the source. Edited May 13, 2021 by andr0xy Link to comment
iMonkr Posted May 13, 2021 Author Share Posted May 13, 2021 5 minutes ago, andr0xy said: Hello and welcome to the forums. source, in this case, is not an element. You will need to bind the key when the resource starts and also when a player joins the server. If it's client-side, just remove the source. bro if i remove source in client side, then what should i add bro or what should i do in server side. can you explain bro, i am new to scripting Link to comment
Other Languages Moderators androksi Posted May 13, 2021 Other Languages Moderators Share Posted May 13, 2021 Client-side has no source variable, it's called localPlayer - unless you are handling it inside an event. Some functions, like bindKey, don't require a player element, you can just ignore it. Remember that you can always check MTA Wiki to learn how to use such function and/or event. On server-side, you must specify the player: Spoiler function myFunction(player, key, keyState) outputChatBox("I pressed " .. key .. " | State: " .. keyState, player, 0, 255, 0) end addEventHandler("onResourceStart", resourceRoot, function() local players = getElementsByType("player") -- Getting all players elements for i = 1, #players do local player = players[i] bindKey(player, "F2", "down", myFunction) end end) addEventHandler("onPlayerJoin", root, function() -- the "source" of this event is the player who joined the server bindKey(source, "F2", "down", myFunction) end) On client-side, it's simple. Spoiler function myFunction(key, keyState) outputChatBox("I pressed " .. key .. " | State: " .. keyState) end bindKey("F2", "down", myFunction) Link to comment
iMonkr Posted May 13, 2021 Author Share Posted May 13, 2021 23 minutes ago, andr0xy said: Client-side has no source variable, it's called localPlayer - unless you are handling it inside an event. Some functions, like bindKey, don't require a player element, you can just ignore it. Remember that you can always check MTA Wiki to learn how to use such function and/or event. On server-side, you must specify the player: Reveal hidden contents function myFunction(player, key, keyState) outputChatBox("I pressed " .. key .. " | State: " .. keyState, player, 0, 255, 0) end addEventHandler("onResourceStart", resourceRoot, function() local players = getElementsByType("player") -- Getting all players elements for i = 1, #players do local player = players[i] bindKey(player, "F2", "down", myFunction) end end) addEventHandler("onPlayerJoin", root, function() -- the "source" of this event is the player who joined the server bindKey(source, "F2", "down", myFunction) end) On client-side, it's simple. Reveal hidden contents function myFunction(key, keyState) outputChatBox("I pressed " .. key .. " | State: " .. keyState) end bindKey("F2", "down", myFunction) ah wait a minute bro client side.... testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) bindKey("e", "down", function() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end) here is my script bro! it works perfectly.... but i have a doubt bro, if the object is been freezed by a player will it be applicable for other players? Link to comment
Tekken Posted May 14, 2021 Share Posted May 14, 2021 It will not be as you freeze it on client side (the PC of the player that frozen the object), so you should do it on server side: testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) function freezeObject() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end addEventHandler("onResourceStart", resourceRoot, function() --bind for all online players on resource start for i,player in pairs(getElementsByType("player")) do bindKey(player, "e", "down", freezeObject) end end); addEventHandler("onPlayerJoin", root, function() --bind for player on he joins bindKey(source, "e", "down", freezeObject) end); Server-side^ Sorry for the messy code I’m on phone Link to comment
iMonkr Posted May 14, 2021 Author Share Posted May 14, 2021 30 minutes ago, Tekken said: It will not be as you freeze it on client side (the PC of the player that frozen the object), so you should do it on server side: testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) function freezeObject() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end addEventHandler("onResourceStart", resourceRoot, function() --bind for all online players on resource start for i,player in pairs(getElementsByType("player")) do bindKey(player, "e", "down", freezeObject) end end); addEventHandler("onPlayerJoin", root, function() --bind for player on he joins bindKey(source, "e", "down", freezeObject) end); Server-side^ Sorry for the messy code I’m on phone okaie thank you bruh 45 minutes ago, Tekken said: It will not be as you freeze it on client side (the PC of the player that frozen the object), so you should do it on server side: testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) function freezeObject() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end addEventHandler("onResourceStart", resourceRoot, function() --bind for all online players on resource start for i,player in pairs(getElementsByType("player")) do bindKey(player, "e", "down", freezeObject) end end); addEventHandler("onPlayerJoin", root, function() --bind for player on he joins bindKey(source, "e", "down", freezeObject) end); Server-side^ Sorry for the messy code I’m on phone bro i have a doubt! can we set nearby object?? i mean we need to be near the object to run this function, for that what element i should use bro Link to comment
SpecT Posted May 14, 2021 Share Posted May 14, 2021 You can use the function getDistanceBetweenPoints3D to get the distance between the player and the object. For example: if getDistanceBetweenPoints3D(pX, pY, pZ, oX, oY, oZ) <= 50 then -- first 3 args are player's position, the other 3 args are object's position -- do your stuff end Link to comment
iMonkr Posted May 14, 2021 Author Share Posted May 14, 2021 10 minutes ago, SpecT said: You can use the function getDistanceBetweenPoints3D to get the distance between the player and the object. For example: if getDistanceBetweenPoints3D(pX, pY, pZ, oX, oY, oZ) <= 50 then -- first 3 args are player's position, the other 3 args are object's position -- do your stuff end Quote testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) function freezeObject() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end can you help me with it? Link to comment
SpecT Posted May 14, 2021 Share Posted May 14, 2021 (edited) 34 minutes ago, iMonkr said: can you help me with it? Well I can but you won't learn anything expecting someone else to do it for you. You will need getElementPosition to get the x, y and z coordinates for the player and the object and then compare them with the getDistanceBetweenPoints3D as I have shown you in the post above. At least give it a try and show us what you tried and we will correct it if it's needed. Edited May 14, 2021 by SpecT Link to comment
iMonkr Posted May 14, 2021 Author Share Posted May 14, 2021 (edited) 11 minutes ago, SpecT said: Well I can but you won't learn anything expecting someone else to do it for you. You will need getElementPosition to get the x, y and z coordinates for the player and the object and then compare them with the getDistanceBetweenPoints3D as I have shown you in the post above. At least give it a try and show us what you tried and we will correct it if it's needed. Okaie bro you are right, but wait bro lemme show my script. I used onMarketHit to run the function, but it's not working. Can you check what error?? Here is my script Quote testdoor = createObject(1499, 1580.03149, -1631.73987, 12.38281) playerMarker = createMarker(1580.03149, -1631.73987, 12.38281, "cylinder", 5, 0, 0, 0, 0) function freezeObject() local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end addEventHandler("onResourceStart", resourceRoot, function() --bind for all online players on resource start for i,player in pairs(getElementsByType("player")) do bindKey(player, "e", "down", freezeObject) end end) addEventHandler("onMarkerHit", playerMarker, function() bindKey(source, "e", "down", freezeObject) end) Edited May 14, 2021 by iMonkr Link to comment
SpecT Posted May 14, 2021 Share Posted May 14, 2021 (edited) You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. Edited May 14, 2021 by SpecT 1 Link to comment
iMonkr Posted May 14, 2021 Author Share Posted May 14, 2021 1 minute ago, SpecT said: You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. Oh okaiee bro thank you 6 minutes ago, SpecT said: You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. not working bro 10 minutes ago, SpecT said: You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. sorrie bro its 8 minutes ago, iMonkr said: Oh okaiee bro thank you not working bro sorriee bro i make a mistake, now its working thank you bro Link to comment
iMonkr Posted May 14, 2021 Author Share Posted May 14, 2021 23 minutes ago, SpecT said: You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. what all function should i use to display a text textCreateDisplay textCreateTextItem textDisplayAddText (thats it?) 41 minutes ago, SpecT said: You don't need the onMarkerHit event. Use isElementWithinMarker to check in the freezeObject function if the player is inside the marker. Like this: function freezeObject(thePlayer) if isElementWithinMarker(thePlayer, playerMarker) then local currentFreezeStatus = isElementFrozen ( testdoor ) if currentFreezeStatus then setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have opened the door!",255,255,255) else setElementFrozen ( testdoor, not currentFreezeStatus ) outputChatBox("You have closed the door!",255,255,255) end end end BTW when you paste code here please use the Code button (next to Quote) in the text editor to be easier to read it here. i need to add a text in an object, like if i freeze the object, it should display freezed, if i unfreeze it should display unfreezed. i used textCreateDisplay textCreateTextItem textDisplayAddText these three functions, but its not displaying why? Link to comment
Moderators IIYAMA Posted May 15, 2021 Moderators Share Posted May 15, 2021 Locked, requested by the creator. Link to comment
Recommended Posts