..:D&G:.. Posted April 20, 2015 Share Posted April 20, 2015 Hello, I am trying to do a garage system that saves in mysql, and I got to the part where I need to make a command that opens the garage with a specific id from the database. The command should be something like: /opengarage [iD of garage]. It seems that I can't think today as I've spent 40 minutes on only this function: function openGarageGate(thePlayer, cmd, dbid) local easing = "OutBounce" local time = 2000 local result = mysqlSystem:query_database("SELECT id, x, y, z, interior, dimension, rot1, rot2, rot3 FROM garages") while true do local row = mysqlSystem:fetch_rows(result) if not (row) then break end local garageID = row['id'] local x = row['x'] local y = row['y'] local z = row['z'] local int = row['interior'] local dim = row['dimension'] local rot1 = row['rot1'] local rot2 = row['rot2'] local rot3 = row['rot3'] local value = exports.objectTables:getElement( "object", 17951 ) if value then local vx, vy, vz = getElementPosition(value) local x, y, z = getElementPosition(thePlayer) if getDistanceBetweenPoints3D(x, y, z, vx, vy, vz) <= 30 then -- Garage found local gate = getElementData(garageID, "garageGate") moveObject(gate, time, x+4.9, y+9.6, z, 0, 0, 0, easing) end end end end end addCommandHandler("opengarage", openGarageGate) I know that this is a mess, but maybe it makes you an idea of what I want to do. Thanks. Link to comment
ALw7sH Posted April 20, 2015 Share Posted April 20, 2015 local result = mysqlSystem:query_database("SELECT x, y, z, interior, dimension, rot1, rot2, rot3 FROM garages WHERE id="..dbid.."") You dont have to use "while" and it's better to use the original mta mysql functions Link to comment
..:D&G:.. Posted April 20, 2015 Author Share Posted April 20, 2015 I changed a function a bit and here is what I got: local easing = "OutBounce" local time = 2000 local open = false function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then local gateX, gateY, gateZ = getElementPosition(gate) --moveObject(gate, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(gate, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) open = true else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end end addCommandHandler("opengarage", openGarageGate) The problem is that it doesn't open the garage gate, I get like a small shake on my screen but that's all. It also gives me like 10 "your are too far away from the garage" messages, then at the end "you have opened your garage". Any ideas? Link to comment
ALw7sH Posted April 20, 2015 Share Posted April 20, 2015 Idont know but isn't "You are too far away from the garage!" must be outputted if you are not too close for the garage that you are trying to open? but what you did is loop for all objects with id 17950 and check if their id equal to id that you have wrote else output "You are too far away from the garage!" the object should move if "You have opened your garage!" have been outputted because it's under the move object line are sure the positions is not equal to the current object positions? and have you checked if there's any erros on debugscript ? Link to comment
WhoAmI Posted April 20, 2015 Share Posted April 20, 2015 local easing = "OutBounce" local time = 2000 local open = false function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then local gateX, gateY, gateZ = getElementPosition(v) --moveObject(v, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(v, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) open = true return; else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end end addCommandHandler("opengarage", openGarageGate) Try this. Also doing like that open = true You are setting value of this variable to everyone in the server while you are using it s-side. Link to comment
..:D&G:.. Posted April 20, 2015 Author Share Posted April 20, 2015 local easing = "OutBounce" local time = 2000 local open = false function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then local gateX, gateY, gateZ = getElementPosition(v) --moveObject(v, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(v, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) open = true return; else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end end addCommandHandler("opengarage", openGarageGate) Try this. Also doing like that open = true You are setting value of this variable to everyone in the server while you are using it s-side. This only creates lots of "You are too far away from the garage" messages... Link to comment
ALw7sH Posted April 20, 2015 Share Posted April 20, 2015 local easing = "OutBounce" local time = 2000 local open = false local distance = 50 function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) local found = false for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then if getDistanceBetweenPoints3D(getElementPosition(v),getElementPosition(thePlayer)) <= distance then local gateX, gateY, gateZ = getElementPosition(gate) --moveObject(gate, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(gate, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) found = true break else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end if found == false then outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) end end end addCommandHandler("opengarage", openGarageGate) Link to comment
..:D&G:.. Posted April 20, 2015 Author Share Posted April 20, 2015 local easing = "OutBounce" local time = 2000 local open = false local distance = 50 function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) local found = false for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then if getDistanceBetweenPoints3D(getElementPosition(v),getElementPosition(thePlayer)) <= distance then local gateX, gateY, gateZ = getElementPosition(gate) --moveObject(gate, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(gate, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) found = true break else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end if found == false then outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) end end end addCommandHandler("opengarage", openGarageGate) Link to comment
ALw7sH Posted April 20, 2015 Share Posted April 20, 2015 local easing = "OutBounce" local time = 2000 local open = false local distance = 50 function openGarageGate(thePlayer, cmd, garageID) if not garageID then outputChatBox("SYNTAXA: /"..cmd.." [iD Garage]", thePlayer, 255, 255, 0) else local garageID = tonumber(garageID) local found = false for i, v in ipairs (getElementsByType("object")) do if (getElementModel(v) == 17950) then local gate = getElementData(v, "poarta") local dbid = tonumber(getElementData(v, "dbid")) if (dbid == garageID) then local px,py,pz = getElementPosition(thePlayer) local ox,oy,oz = getElementPosition(v) if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then local gateX, gateY, gateZ = getElementPosition(gate) --moveObject(gate, time, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0, easing) moveObject(gate, 17951, 1588.5490234375, -1637.95546875, 16.446516990662, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) found = true break else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end end end end if found == false then outputChatBox("There's no garage with this ID!", thePlayer, 255, 0, 0) end end end addCommandHandler("opengarage", openGarageGate) Link to comment
..:D&G:.. Posted April 20, 2015 Author Share Posted April 20, 2015 Now it kinda works, but the gate doesn't move. There is also no error in debugscript Link to comment
ALw7sH Posted April 20, 2015 Share Posted April 20, 2015 are sure the positions is not equal to the current object positions? Link to comment
..:D&G:.. Posted April 20, 2015 Author Share Posted April 20, 2015 Yes, I am sure.. local gateX, gateY, gateZ = getElementPosition(gate) moveObject(gate, 17951, gateX, gateY, gateZ+3, -90, 0, 0) -------------EDIT--------------- When I made a separate gate, it moves and everything works. Also, the script is getting the possition of the gate. Is it maybe because the gate is attached to the garage using attachElement and I somehow need to update the possition from attachElement? Link to comment
..:D&G:.. Posted April 21, 2015 Author Share Posted April 21, 2015 So, instead of moveObject I decided to go with setElementAttachedOffsets but the gate just disappears... if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then local gateX, gateY, gateZ = getElementPosition(gate) local rotX, roxY, rotZ = getElementRotation(gate) setElementAttachedOffsets(gate, gateX, gateY+2, gateZ+2, rotX, -90, rotZ) --moveObject(gate, 2000, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0) -- moveObject(gate1, 17951, gateX, gateY, gateZ+3, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) found = true break else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end Any ideas? Link to comment
ALw7sH Posted April 21, 2015 Share Posted April 21, 2015 if getDistanceBetweenPoints3D(ox,oy,oz,px,py,pz) <= distance then local gateX, gateY, gateZ, rotX, roxY, rotZ = getElementAttachedOffsets(gate) setElementAttachedOffsets(gate, gateX, gateY+2, gateZ+2, rotX, -90, rotZ) --moveObject(gate, 2000, gateX+4.9, gateY+9.6, gateZ, 0, 0, 0) -- moveObject(gate1, 17951, gateX, gateY, gateZ+3, -90, 0, 0) outputChatBox("You have opened your garage!", thePlayer, 0, 255, 0) found = true break else outputChatBox("You are too far away from the garage!", thePlayer, 255, 0, 0) end Link to comment
..:D&G:.. Posted April 21, 2015 Author Share Posted April 21, 2015 It worked, thanks a lot for help 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