Jump to content

open command


Actimel

Recommended Posts

Can someone help me to bind key on this script for opening gates?

code for command

local opencommand = "open"  --open command
local closecommand = "close" --close command


local GateObject = 980  --object
local OpenTime = 3000 --opening second
local rotX, rotY, rotZ = 0, 0, 270 --rotations 
local Defx, Defy, defz = -489.20001220703,-562.40002441406,27.299999237061 --closed position 
local opx, opy, opz = -489.20001220703,-562.40002441406,21.7  --opened position

--create the gate (DO NOT EDIT) 
local ggate = createObject ( GateObject, Defx, Defy, defz, rotX, rotY, rotZ)

--DO NOT EDIT
function gateopen ( p )
    if isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(p)), aclGetGroup(ACL)) then
        moveObject (ggate, OpenTime, opx, opy, opz)
        outputChatBox("Admin base gate opened", p,0,255,0,true)
    else
        outputChatBox("Sorry you dont have permission to do that!", p,0,255,0,true)
    end
end
addCommandHandler( opencommand, gateopen)
 
function gateclose ( p )
    if isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(p)), aclGetGroup(ACL)) then
        moveObject (ggate, OpenTime, Defx, Defy, defz)
        outputChatBox("Admin base gate closed", p,0,255,0,true)
    else
        outputChatBox("Sorry you dont have permission to do that!", p,0,255,0,true)
    end
end
addCommandHandler( closecommand, gateclose)

 

Link to comment
function handlerFunction(thePlayer)
	local oX, oY, oZ = getElementPosition(ggate)
	if oZ ~= defz then
		gateclose(thePlayer)
	else
		gateopen(thePlayer)
	end
end

addEventHandler("onResourceStart," getResourceRootElement(), function()
	for i, k ipairs(getElementsByType("player")) do
		bindKey(k, "down", handlerFunction)
	end
end)

addEventHandler("onPlayerJoin", getRootElement(), function()
	bindKey(source, "down", handlerFunction)
end)

I think this one should work.

  • Like 1
Link to comment
10 minutes ago, NeXuS™ said:

function handlerFunction(thePlayer)
	local oX, oY, oZ = getElementPosition(ggate)
	if oZ ~= defz then
		gateclose(thePlayer)
	else
		gateopen(thePlayer)
	end
end

addEventHandler("onResourceStart," getResourceRootElement(), function()
	for i, k ipairs(getElementsByType("player")) do
		bindKey(k, "down", handlerFunction)
	end
end)

addEventHandler("onPlayerJoin", getRootElement(), function()
	bindKey(source, "down", handlerFunction)
end)

I think this one should work.

doesnt work

 

Link to comment
function handlerFunction(thePlayer)
	local oX, oY, oZ = getElementPosition(ggate)
	if oZ ~= defz then
		gateclose(thePlayer)
	else
		gateopen(thePlayer)
	end
end

addEventHandler("onResourceStart," getResourceRootElement(), function()
	for i, k ipairs(getElementsByType("player")) do
		bindKey(k, "K", "down", handlerFunction)
	end
end)

addEventHandler("onPlayerJoin", getRootElement(), function()
	bindKey(source, "K", "down", handlerFunction)
end)

bindKey arguments were :Oed.

Link to comment

You can use /bind key command to bind a command to a key.

/bind j open

When I press j it will execute the open command

it's better to use commandHandlers instead of bindKey because not all players would want to use the same key.

Also instead of using 2 separate functions to open/close the gate just use 1 function so all you need is 1 command

for example

local opencommand = "gate"  --toggle command

local GateObject = 980  --object
local OpenTime = 3000 --opening second
local rotX, rotY, rotZ = 0, 0, 270 --rotations 
local x, y, z = -489.2,-562.4,27.299 --closed position 
local open --Var representing the state of the gate opened = true closed = false


local ggate = createObject ( GateObject, x, y, z, rotX, rotY, rotZ)

function toggleGate ( p )
	if isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(p)), aclGetGroup(ACL)) then
		if open then
			moveObject (ggate, OpenTime, x, y, z)
		else
			moveObject (ggate, OpenTime, x, y, z-5.5)
		end
		local state = not open and "opened." or "closed." -- check if gate opened and set the text to opened/closed
		outputChatBox("Admin base gate "..state, p,0,255,0,true)
		open = not open --toggle the open var between true/false	
	else
		outputChatBox("Sorry, you dont have permission to do that!", p,0,255,0,true)
	end
end
addCommandHandler( opencommand, toggleGate)

 

Link to comment
4 minutes ago, Mr.Loki said:

You can use /bind key command to bind a command to a key.

/bind j open

When I press j it will execute the open command

it's better to use commandHandlers instead of bindKey because not all players would want to use the same key.

Also instead of using 2 separate functions to open/close the gate just use 1 function so all you need is 1 command

for example


local opencommand = "gate"  --toggle command

local GateObject = 980  --object
local OpenTime = 3000 --opening second
local rotX, rotY, rotZ = 0, 0, 270 --rotations 
local x, y, z = -489.2,-562.4,27.299 --closed position 
local open --Var representing the state of the gate opened = true closed = false


local ggate = createObject ( GateObject, x, y, z, rotX, rotY, rotZ)

function toggleGate ( p )
	if isObjectInACLGroup("user." .. getAccountName(getPlayerAccount(p)), aclGetGroup(ACL)) then
		if open then
			moveObject (ggate, OpenTime, x, y, z)
		else
			moveObject (ggate, OpenTime, x, y, z-5.5)
		end
		local state = not open and "opened." or "closed." -- check if gate opened and set the text to opened/closed
		outputChatBox("Admin base gate "..state, p,0,255,0,true)
		open = not open --toggle the open var between true/false	
	else
		outputChatBox("Sorry, you dont have permission to do that!", p,0,255,0,true)
	end
end
addCommandHandler( opencommand, toggleGate)

 

its not for other players i need. i need just for private base for example

 

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