Jump to content

bad argument @ 'bindkey'


Recommended Posts

Posted (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 by iMonkr
Link to post
Posted (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 by andr0xy
Link to post
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 post

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

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

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 post
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 post
Posted (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 by SpecT
Link to post
Posted (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 by iMonkr
Link to post
Posted (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.Js6eCDI.png

Edited by SpecT
  • Like 1
Link to post
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.Js6eCDI.png

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

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 post
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.Js6eCDI.png

what all function should i use to display a text

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

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 post
  • IIYAMA locked this topic
Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...