Firespider Posted Wednesday at 20:41 Share Posted Wednesday at 20:41 Hello, there is a small problem: the code does not break out of the cycle when I write the word break, here is the code (But what is before the break is executed, only after the break does not end the cycle) function gethere(player, cmd, ID) if getElementData(player, "Player.AdminLevel") > 1 then local players = getElementsByType ( "player" ) if tonumber(ID) then for i, p in ipairs(players) do if getElementData(p, "Player.ID") == tonumber(ID) then local x, y, z = getElementPosition(player) setElementPosition(p, x, y, z) break else outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff Rossz ID-t adtál meg.", player, 255, 255, 255, true) end end else outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff /gethere [ID]", player, 255, 255, 255, true) end end end addCommandHandler("gethere", gethere, false, false) Link to comment
Moderators IIYAMA Posted Wednesday at 21:50 Moderators Share Posted Wednesday at 21:50 1 hour ago, Firespider said: Hello, there is a small problem: the code does not break out of the cycle when I write the word break, here is the code (But what is before the break is executed, only after the break does not end the cycle) When use the keyword break, it jumps from: for i, p in ipairs(players) do if getElementData(p, "Player.ID") == tonumber(ID) then local x, y, z = getElementPosition(player) setElementPosition(p, x, y, z) break -- FROM HERE else outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff Rossz ID-t adtál meg.", player, 255, 255, 255, true) end end -- TO HERE The following code prints: A, D for i=1, 10 do if true then print("A") break print("B") else print("C") end end print("D") Run here to test: https://onecompiler.com/lua/42zdspdkb Not sure what exactly the problem is that you are facing, but in your current code you could also use the return keyword to immediately stop the function. 1 Link to comment
Firespider Posted Thursday at 15:33 Author Share Posted Thursday at 15:33 Now the problem is that the players have an ID, but the system only detects the individual ID and you can only TP on it, etc., etc. Link to comment
Moderators IIYAMA Posted Thursday at 17:48 Moderators Share Posted Thursday at 17:48 2 hours ago, Firespider said: Now the problem is that the players have an ID, but the system only detects the individual ID and you can only TP on it, etc., etc. Maybe you should switch over to teleport by player (partial) name? https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName Makes things a little bit easier. Link to comment
Firespider Posted Thursday at 18:01 Author Share Posted Thursday at 18:01 But that returns a name, doesn't it? Link to comment
Moderators IIYAMA Posted Thursday at 18:10 Moderators Share Posted Thursday at 18:10 6 minutes ago, Firespider said: But that returns a name, doesn't it? No, it returns a player<element> or nil function getPlayerFromPartialName(name) local name = name and name:gsub("#%x%x%x%x%x%x", ""):lower() or nil if name then for _, player in ipairs(getElementsByType("player")) do local name_ = getPlayerName(player):gsub("#%x%x%x%x%x%x", ""):lower() if name_:find(name, 1, true) then return player end end end end local name = "IIYAMA" local player = getPlayerFromPartialName ( name ) if player then -- Found else -- Not found end Link to comment
Firespider Posted Thursday at 18:20 Author Share Posted Thursday at 18:20 Yes, but does it need a name argument? However, I would like to solve it with ID Link to comment
Moderators IIYAMA Posted Thursday at 19:33 Moderators Share Posted Thursday at 19:33 1 hour ago, Firespider said: Yes, but does it need a name argument? However, I would like to solve it with ID It does indeed In that case, could you explain the following with some more context? 3 hours ago, Firespider said: problem is that the players have an ID, but the system only detects the individual ID It is unclear what an `individual ID` is in the current context. Link to comment
Firespider Posted Thursday at 20:01 Author Share Posted Thursday at 20:01 So I have the problem that every player gets an ID with setElementData when, for example, ID 1 appears based on the primary row of the SQL table But when the command is entered on the server, the system only works with a player with a specific ID, e.g. we can TP the player with a specific ID, etc. Link to comment
Moderators IIYAMA Posted Thursday at 22:27 Moderators Share Posted Thursday at 22:27 2 hours ago, Firespider said: So I have the problem that every player gets an ID with setElementData when, for example, ID 1 appears based on the primary row of the SQL table But when the command is entered on the server, the system only works with a player with a specific ID, e.g. we can TP the player with a specific ID, etc. So you mean the user case when no ID is assigned to a new player? Based on what criteria are you assigning the ID? Link to comment
Firespider Posted Friday at 09:13 Author Share Posted Friday at 09:13 (edited) This is how I add the ID to the player addEventHandler("onPlayerJoin", root, function () local serial = getPlayerSerial(source) local player = source local dq = dbQuery(db, "SELECT * FROM characters WHERE serial=?", serial) local result = dbPoll(dq, 250) -- Ne korlátozd az eredmények számát if result and #result > 0 then -- Az adatbázisban a munkát tároló oszlop neve local name = tostring(result[1]["name"]) local WhitoutSpace = string.gsub(name, " ", "_") local id = result[1]["ID"] pos = fromJSON(result[1]["pos"]) local skin = result[1]["skinID"] local money = result[1]["Money"] setPlayerMoney(player, money) setElementData(player, "Player.ID", id) local data = getElementData(player, "Player.ID") setPlayerName(player, WhitoutSpace) setElementModel(player, skin) setElementPosition(player, pos[1], pos[2], pos[3]) end end ) Edited Friday at 09:14 by Firespider Link to comment
Firespider Posted Friday at 09:16 Author Share Posted Friday at 09:16 (edited) At each entry, you set a SetElementData for the value of the ID table Edited Friday at 09:17 by Firespider Link to comment
Moderators IIYAMA Posted Friday at 18:00 Moderators Share Posted Friday at 18:00 8 hours ago, Firespider said: At each entry, you set a SetElementData for the value of the ID table And at what point do you save the character? After filling in info of some sort? 8 hours ago, Firespider said: local dq = dbQuery(db, "SELECT * FROM characters WHERE serial=?", serial) Are you using SQLite or MySQL? Link to comment
Firespider Posted Friday at 18:02 Author Share Posted Friday at 18:02 (edited) SQL i think Edited Friday at 18:05 by Firespider Link to comment
Moderators IIYAMA Posted Friday at 18:30 Moderators Share Posted Friday at 18:30 25 minutes ago, Firespider said: MYSQL In that case you can try to work with the following building blocks. (untested) local userName = "root" local password = "root" local host = "127.0.0.1" local db = dbConnect( "mysql", host, userName, password, "multi_statements=1" -- optional ) -- ... local player -- = ??? local serial = 1234567890 if db then dbQuery(onRegisterPlayer, {player}, db, [[ INSERT INTO characters (serial) VALUES (?) ON DUPLICATE KEY UPDATE id=VALUES(id); ]], serial) end -- ... ---@param queryHandle userdata ---@param player userdata function onRegisterPlayer(queryHandle, player) if not isElement(player) then return end local multipleResults = true -- return the id of the inserted row local result = dbPoll(queryHandle, -1, multipleResults) if not result then --- Something went wrong error("Failed to register player") return end iprint("Table", result[1]) iprint("Affected rows", result[2]) iprint("Last insert id", result[3]) end Link to comment
Firespider Posted Friday at 18:36 Author Share Posted Friday at 18:36 Could you explain what this code does? Link to comment
Moderators IIYAMA Posted Friday at 18:50 Moderators Share Posted Friday at 18:50 23 minutes ago, Firespider said: Could you explain what this code does? It inserts a row into the database and returns the generated primary key. But most of it are just examples for you to apply in to your own code. (also not tested, I haven't combined insert + update with the multipleResults statement myself) If something is unclear, please be specific in which part or function you do not understand. Then I will explain how that part/function works. Link to comment
Firespider Posted Friday at 19:01 Author Share Posted Friday at 19:01 So I threw my previous code into the SQL bin? What did I send? Link to comment
Firespider Posted Friday at 19:37 Author Share Posted Friday at 19:37 I deleted the break and it was fine, but it always says 3 times that I entered the wrong ID, but the commands still work Link to comment
Moderators IIYAMA Posted Friday at 19:40 Moderators Share Posted Friday at 19:40 1 minute ago, Firespider said: but it always says 3 times that I entered the wrong ID On 27/11/2024 at 21:41, Firespider said: if tonumber(ID) then You should move this condition before the loop and if it fails, do the outputChatBox + return statement. Link to comment
Moderators IIYAMA Posted Friday at 20:44 Moderators Share Posted Friday at 20:44 41 minutes ago, Firespider said: I using that Oke, in that case please show me the code so that I can check if it is applied correctly. Link to comment
Firespider Posted Friday at 21:07 Author Share Posted Friday at 21:07 function tp(player, cmd, ID) if getElementData(player, "Player.AdminLevel") > 1 then local players = getElementsByType ( "player" ) if tonumber(ID) then for i, p in ipairs(players) do print(getElementData(p, "Player.ID")) if getElementData(p, "Player.ID") == tonumber(ID) then local x, y, z = getElementPosition(p) setElementPosition(player, x, y, z) else outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff Rossz ID-t adtál meg.", player, 255, 255, 255, true) end end end end end Link to comment
Moderators IIYAMA Posted Friday at 21:20 Moderators Share Posted Friday at 21:20 12 minutes ago, Firespider said: function tp(player, cmd, ID) if getElementData(player, "Player.AdminLevel") > 1 then local players = getElementsByType ( "player" ) if tonumber(ID) then for i, p in ipairs(players) do print(getElementData(p, "Player.ID")) if getElementData(p, "Player.ID") == tonumber(ID) then local x, y, z = getElementPosition(p) setElementPosition(player, x, y, z) else outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff Rossz ID-t adtál meg.", player, 255, 255, 255, true) end end end end end ---@param ID number ---@return player | nil function findPlayerByID(ID) local players = getElementsByType ( "player" ) for i, p in ipairs(players) do if getElementData(p, "Player.ID") == ID then return p end end end ---@param player element ---@param cmd string ---@param rawID string | nil function tp(player, cmd, rawID) -- Admin level check if getElementData(player, "Player.AdminLevel") <= 1 then return end local ID = tonumber(rawID) if type(ID) ~= "number" then outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff /gethere [ID]", player, 255, 255, 255, true) return end local targetPlayer = findPlayerByID(ID) if not targetPlayer then outputChatBox("#000000<#910e07Wise#ffffffRolePlay#000000> #ffffff Rossz ID-t adtál meg.", player, 255, 255, 255, true) return end local x, y, z = getElementPosition(targetPlayer) setElementPosition(player, x, y, z) end 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