Firespider Posted June 25, 2023 Share Posted June 25, 2023 Hello, I want to write a border system, but when the gate opens, it doesn't close again for some reason. local sx, sy = guiGetScreenSize(); local relx,rely = sx/1920,sy/1080; local GateMenu = false local GateStatus = false local TheGate = createObject(1468, 51.62455, -1524.62549, 5.05399, 0, 0, 90); local TheSensor = createColSphere(55.60476, -1524.89941, 5.00539, 3); color = tocolor(10, 20, 30, 255) addCommandHandler("devmode", function() setDevelopmentMode(true); end ) addEventHandler("onClientRender", root, function() if (GateMenu) then dxDrawRectangle(sx*.35, sy*.4, sx*.3, sy*.05, tocolor(10, 20, 10, 255)) -- fejléc dxDrawRectangle(sx*.35, sy*.4, sx*.3, sy*.40, tocolor(20, 30, 40, 200)) -- Alap dxDrawRectangle(sx*.42, sy*.6, sx*.15, sy*.08, color1) -- Gomb(Megvásárlás) dxDrawText('Határ rendszer', sx*.70, sy*.8, sx*.3, sy*.05, relx2, rely*2, "default-bold", "center", "center", false, false, false) dxDrawText('Fizesd ki a határt ára: 5000 FT', sx*.70, sy*.9, sx*.3, sy*.05, relx2, rely*2, "default-bold", "center", "center", false, false, false) dxDrawText('Ki fizetés', sx*.84, sy*.9, sx*.15, sy*.380, relx2, rely*2, "default-bold", "center", "center", false, false, false) GlowedButton(); end end ) bindKey ("m", "down", function() showCursor( not isCursorShowing() ) end) function isMouseInPosition ( x, y, width, height )-- Kurzor lekérése if ( not isCursorShowing( ) ) then return false end local sx, sy = guiGetScreenSize ( ) local cx, cy = getCursorPosition ( ) local cx, cy = ( cx * sx ), ( cy * sy ) return ( ( cx >= x and cx <= x + width ) and ( cy >= y and cy <= y + height ) ) end function GlowedButton() if (GateMenu) then if (isMouseInPosition(sx*.42, sy*.6, sx*.15, sy*.08)) then color1 = color else color1 = tocolor(5, 10, 15, 255) end end end local veh = getO addEventHandler("onClientElementColShapeHit", root, function(hitElement, matchingDimension) if matchingDimension then if hitElement == TheSensor then GateMenu = true end end end) addEventHandler("onClientElementColShapeLeave", root, function(hitElement, matchingDimension) if matchingDimension then if hitElement == TheSensor then GateMenu = false end end end) addEventHandler("onClientClick", root, function(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedElement) if GateMenu and button == "left" and state == "down" then if isMouseInPosition(sx*.42, sy*.6, sx*.15, sy*.08) then GateMenu = false moveObject(TheGate, 5000, 51.62455, -1524.62549, 0) GateStatus = true triggerEvent("Reset", localPlayer) end end end ) addEvent("Reset", true) addEventHandler("Reset", root, function() if (GateStatus) then moveObject(TheGate, 100000, 1468, 51.62455, -1524.62549, 5.05399) end end ) Link to comment
FLUSHBICEPS Posted July 1, 2023 Share Posted July 1, 2023 the gate is not closing because the "Reset" event is being triggered immediately after the gate is opened local GateCloseTimer = nil -- variable to hold the timer addEventHandler("onClientClick", root, function(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedElement) if GateMenu and button == "left" and state == "down" then if isMouseInPosition(sx*.42, sy*.6, sx*.15, sy*.08) then GateMenu = false moveObject(TheGate, 5000, 51.62455, -1524.62549, 0) GateStatus = true if isTimer(GateCloseTimer) then killTimer(GateCloseTimer) end GateCloseTimer = setTimer(function() triggerEvent("Reset", localPlayer) end, 5000, 1) -- This is the delay before the gate closes end end end) addEvent("Reset", true) addEventHandler("Reset", root, function() if (GateStatus) then moveObject(TheGate, 5000, 51.62455, -1524.62549, 5.05399) GateStatus = false end end ) Link to comment
ExMohmD Posted August 4, 2023 Share Posted August 4, 2023 It seems like the issue is with the event handler for resetting the gate. The onClientClick event handler is triggering the gate to open but not closing it. The Reset event is supposed to close the gate, but there's a problem with its implementation. To fix this, you should modify the Reset event handler to close the gate when it is triggered. Also, it's better to use a single event handler for both opening and closing the gate. Here's the revised code: local sx, sy = guiGetScreenSize(); local relx, rely = sx / 1920, sy / 1080; local GateMenu = false local GateStatus = false local TheGate = createObject(1468, 51.62455, -1524.62549, 5.05399, 0, 0, 90); local TheSensor = createColSphere(55.60476, -1524.89941, 5.00539, 3); color = tocolor(10, 20, 30, 255) color1 = tocolor(5, 10, 15, 255) addCommandHandler("devmode", function() setDevelopmentMode(true); end ) addEventHandler("onClientRender", root, function() if (GateMenu) then dxDrawRectangle(sx * .35, sy * .4, sx * .3, sy * .05, tocolor(10, 20, 10, 255)) -- fejléc dxDrawRectangle(sx * .35, sy * .4, sx * .3, sy * .40, tocolor(20, 30, 40, 200)) -- Alap dxDrawRectangle(sx * .42, sy * .6, sx * .15, sy * .08, color1) -- Gomb(Megvásárlás) dxDrawText('Határ rendszer', sx * .70, sy * .8, sx * .3, sy * .05, relx2, rely * 2, "default-bold", "center", "center", false, false, false) dxDrawText('Fizesd ki a határt ára: 5000 FT', sx * .70, sy * .9, sx * .3, sy * .05, relx2, rely * 2, "default-bold", "center", "center", false, false, false) dxDrawText('Ki fizetés', sx * .84, sy * .9, sx * .15, sy * .380, relx2, rely * 2, "default-bold", "center", "center", false, false, false) GlowedButton(); end end ) bindKey("m", "down", function() showCursor(not isCursorShowing()) end ) function isMouseInPosition(x, y, width, height) -- Kurzor lekérése if (not isCursorShowing()) then return false end local sx, sy = guiGetScreenSize() local cx, cy = getCursorPosition() local cx, cy = (cx * sx), (cy * sy) return ((cx >= x and cx <= x + width) and (cy >= y and cy <= y + height)) end function GlowedButton() if (GateMenu) then if (isMouseInPosition(sx * .42, sy * .6, sx * .15, sy * .08)) then color1 = color else color1 = tocolor(5, 10, 15, 255) end end end addEventHandler("onClientElementColShapeHit", root, function(hitElement, matchingDimension) if matchingDimension then if hitElement == TheSensor then GateMenu = true end end end) addEventHandler("onClientElementColShapeLeave", root, function(hitElement, matchingDimension) if matchingDimension then if hitElement == TheSensor then GateMenu = false end end end) addEventHandler("onClientClick", root, function(button, state, absoluteX, absoluteY, worldX, worldY, worldZ, clickedElement) if GateMenu and button == "left" and state == "down" then if isMouseInPosition(sx * .42, sy * .6, sx * .15, sy * .08) then GateMenu = false if GateStatus then moveObject(TheGate, 5000, 51.62455, -1524.62549, 0) GateStatus = false else moveObject(TheGate, 5000, 1468, 51.62455, -1524.62549, 5.05399) GateStatus = true end end end end) Now the gate should open when you click the button and close again when you click it again. The Reset event is not needed in this case, as we can handle the gate's opening and closing in the same event handler. 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