w3rt1x Posted December 17, 2022 Share Posted December 17, 2022 (edited) Quote function createPanel(commandName) local window = guiCreateWindow(532, 373, 232, 171, "TELEPORTER POSITION", false) guiWindowSetSizable(window, false) local button_1 = guiCreateButton(141, 99, 64, 25, "EXIT", false, window) -- guiSetAlpha(button_1, 0.42) -- local button_2 = guiCreateButton(32, 99, 64, 25, "TELEPORT", false, window) local edit_box = guiCreateEdit(42, 37, 153, 21, "0,0,0", false, window) showCursor(true) addEventHandler("onClientGUIClick", button_1, function() if isElement(window) then destroyElement(window) showCursor(false) end end) addEventHandler("onClientGUIClick", button_2, function(player) local x, y ,z = guiGetText(edit_box) setElementPosition(player, x, y, z) end) end addCommandHandler("tp", createPanel) Edited December 17, 2022 by w3rt1x Link to comment
Trust aka Tiffergan Posted December 17, 2022 Share Posted December 17, 2022 function createPanel(commandName) local window = guiCreateWindow(532, 373, 232, 171, "TELEPORTER POSITION", false) guiWindowSetSizable(window, false) local button_1 = guiCreateButton(141, 99, 64, 25, "EXIT", false, window) -- guiSetAlpha(button_1, 0.42) -- local button_2 = guiCreateButton(32, 99, 64, 25, "TELEPORT", false, window) local edit_box = guiCreateEdit(42, 37, 153, 21, "0,0,0", false, window) showCursor(true) addEventHandler("onClientGUIClick", button_1, function() if isElement(window) then destroyElement(window) showCursor(false) end end) addEventHandler("onClientGUIClick", button_2, function(player) local x, y ,z = string.match(guiGetText(edit_box), "(%d+), (%d+), (%d+)") x, y, z = tonumber(x), tonumber(y), tonumber(z) setElementPosition(player, x, y, z) end) end addCommandHandler("tp", createPanel) To use this script, the player can enter the desired coordinates in the format "x, y, z" in the edit box and click the "TELEPORT" button. This will teleport the player to the specified coordinates. 1 Link to comment
DiSaMe Posted December 17, 2022 Share Posted December 17, 2022 (edited) You can use split to split the string into tokens and convert each of them to numbers. local xyz_str = guiGetText(edit_box) local xyz = split(xyz_str, ",") -- xyz is now a table of tokens if #xyz == 3 then -- check if there are exactly 3 coordinates local x, y, z = tonumber(xyz[1]), tonumber(xyz[2]), tonumber(xyz[3]) if x and y and z then -- check if each coordinate is a valid number -- code to execute if coordinates were parsed successfully else -- code to execute if not all coordinates are valid numbers end else -- code to execute if there aren't exactly 3 coordinates end 38 minutes ago, LOOah said: local x, y ,z = string.match(guiGetText(edit_box), "(%d+), (%d+), (%d+)") This is a good start, but the space characters have to match exactly, and the numbers can only be non-negative integers. I was going to improve the pattern to allow arbitrary number of spaces, but then I remembered non-integers and negative numbers and thought it would be too much hassle to get it right. With split, we allow tonumber to take care of all number formats and space characters. Edited December 17, 2022 by Reyomin 1 Link to comment
Trust aka Tiffergan Posted December 17, 2022 Share Posted December 17, 2022 You are correct that the string.match() function is not the best choice for parsing coordinates in this scenario. Splitting the string into tokens using string.split() and then converting each token to a number using tonumber() is a more robust solution. Here is an example of how you can use string.split() and tonumber() to parse the coordinates string entered in the edit box and store the x, y, and z values in separate variables: local xyz_str = guiGetText(edit_box) local xyz = split(xyz_str, ",") -- xyz is now a table of tokens if #xyz == 3 then -- check if there are exactly 3 coordinates local x, y, z = tonumber(xyz[1]), tonumber(xyz[2]), tonumber(xyz[3]) if x and y and z then -- check if each coordinate is a valid number -- code to execute if coordinates were parsed successfully else -- code to execute if not all coordinates are valid numbers end else -- code to execute if there aren't exactly 3 coordinates end This code will split the coordinates string entered in the edit box into separate tokens using string.split(), and then convert each token to a number using tonumber(). It also checks that there are exactly 3 coordinates and that each coordinate is a valid number before proceeding. You can then use the x, y, and z variables to teleport the player to the specified coordinates using setElementPosition(), as in the previous example. setElementPosition(player, x, y, z) 1 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