Jump to content

question getplayername


iiv03

Recommended Posts

  • Moderators
-- SERVER

addCommandHandler("give", function(player, cmd, ...)
	local args = {...} -- example: bot robot unknown 9999
	-- we know: the last arg is the amount, a number --> and every arg behind the amount are names
	
	local args_count = #args
	if args_count < 2 then return outputChatBox("USE: /"..cmd.." name[name ... name] amount", player) end -- minimum need 2 argument
	
	local amount = tonumber(args[args_count])
	if not amount or amount < 1 then return outputChatBox("Invalid amount.", player) end
	
	-- loop players (amount (last argument in table) excluded)
	for i = 1, args_count-1 do
		local name = args[i]
		local target_player = getPlayerFromPartialName(name)
		
		if isElement(target_player) then
			outputChatBox("Something gived to " .. getPlayerName(target_player), player)
		else
			outputChatBox("Player " .. name .. " not found.", player)
		end
	end
end)

-- https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName
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

 

You mean something like this?

  • Like 1
Link to comment
36 minutes ago, stPatrick said:

-- SERVER

addCommandHandler("give", function(player, cmd, ...)
	local args = {...} -- example: bot robot unknown 9999
	-- we know: the last arg is the amount, a number --> and every arg behind the amount are names
	
	local args_count = #args
	if args_count < 2 then return outputChatBox("USE: /"..cmd.." name[name ... name] amount", player) end -- minimum need 2 argument
	
	local amount = tonumber(args[args_count])
	if not amount or amount < 1 then return outputChatBox("Invalid amount.", player) end
	
	-- loop players (amount (last argument in table) excluded)
	for i = 1, args_count-1 do
		local name = args[i]
		local target_player = getPlayerFromPartialName(name)
		
		if isElement(target_player) then
			outputChatBox("Something gived to " .. getPlayerName(target_player), player)
		else
			outputChatBox("Player " .. name .. " not found.", player)
		end
	end
end)

-- https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName
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

 

You mean something like this?

ofc but this work in two players

if i change value argus_count to 1 will cause a problems ? bcs i'm alone in my local

Edited by xFabel
Link to comment
  • Moderators
46 minutes ago, xFabel said:

hey i'm forget to telling you so if i set command like this

/give bot 5 unknown 3 robot 4

player amount, player amount, player amount smth like this

how?

 

addCommandHandler("give", function(player, cmd, ...)
	local args = {...} -- example: bot 7777 robot 8888 unknown 9999
	-- we know: every 2th arg is the amount

	local args_count = #args
	if args_count < 2 or args_count % 2 ~= 0 then return outputChatBox("USE: /"..cmd.." [name amount ... name amount]", player) end -- minimum need 2 argument
	
	-- loop datas
	for i = 0, args_count/2-1 do
		local name = args[i*2+1]
		local amount = tonumber(args[i*2+2])
		
		if amount and amount > 0 then
			local target_player = getPlayerFromPartialName(name)
			
			if isElement(target_player) then
				outputChatBox(amount .. " gived to " .. getPlayerName(target_player), player)
			else
				outputChatBox("Player " .. name .. " not found.", player)
			end
		else
			outputChatBox("Invalid amount '"..(args[i*2+2]).."' for "..name, player)
		end
	end
end)

-- https://wiki.multitheftauto.com/wiki/GetPlayerFromPartialName
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

 

Edited by stPatrick
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...