Jump to content

Break problem


Firespider

Recommended Posts

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

  • Like 1
Link to comment
  • Moderators
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
  • Moderators
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

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

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 by Firespider
Link to comment
  • Moderators
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
  • Moderators
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
  • Moderators
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
  • Moderators

 

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

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