Server-side
local pendentes = {} -- Lista de players que tem solicitação de skin pendente. Formato: [playerElement] = skinID,
local timersPendentes = {} -- Lista com os timers de cada solicitação.
addCommandHandler("mudarskin", function(thePlayer, cmd, playerName, skinID) -- Comando para enviar a solicitação para o jogador.
if not playerName or not skinID then
outputChatBox("Sintaxe: /mudarskin <NomeJogador> <skinID>", thePlayer)
return -- Se faltou especificar o nome do jogador ou o id da skin no comando, cancela a função e nada acontece.
end
local otherPlayer = getPlayerFromPartialName(playerName) -- Obtém o jogador pelo nome.
if otherPlayer then -- Se encontrou o jogador, então:
if pendentes[otherPlayer] then -- Se o jogador já tem uma solicitação na lista, então:
outputChatBox("Este jogador já tem uma solicitação de skin pendente.", thePlayer)
else -- Se o jogador ainda não tem uma solicitação na lista, então:
pendentes[otherPlayer] = tonumber(skinID) -- Adiciona a solicitação dele na lista.
outputChatBox("Você recebeu uma solicitação de skin "..skinID..". Digite /aceitarskin ou /recusarskin", otherPlayer) -- Manda isso pra ele.
timersPendentes[otherPlayer] = setTimer(function() -- Depois de 10 segundos, expira a solicitação.
pendentes[otherPlayer] = nil -- Remove a solicitação da lista.
timersPendentes[otherPlayer] = nil -- Anula a variável do timer, removendo da lista de timers.
outputChatBox("O jogador '"..playerName.."' não respondeu a sua solicitação e ela expirou.", thePlayer)
end, 10000, 1)
end
else -- Se não encontrou o jogador pelo nome, então:
outputChatBox("Não foi encontrado nenhum jogador com o nome '"..playerName.."'", thePlayer)
end
end)
function getPlayerFromPartialName(name) -- Função útil que faz o mesmo que getPlayerFromName, mas o nome não precisa ser exato.
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
function changeSkin(thePlayer, cmd) -- Função de aceitar ou recusar solicitações.
if pendentes[thePlayer] then -- Se o jogador que executou o comando tem alguma solicitação na lista, então:
if cmd == "aceitarskin" then -- Se ele usou o comando /aceitarskin então:
setElementModel (thePlayer, pendentes[thePlayer]) -- Muda a skin dele.
outputChatBox("Você aceitou a solicitação de skin.", thePlayer)
else -- Se ele usou o comando /recusarskin então:
outputChatBox("Você recusou a solicitação de skin.", thePlayer)
end
pendentes[thePlayer] = nil -- Remove a solicitação da lista.
if isTimer(timersPendentes[thePlayer]) then -- Se existe o timer da solicitação, então:
killTimer(timersPendentes[thePlayer]) -- Cancela o timer da solicitação.
timersPendentes[thePlayer] = nil -- Remove o timer da lista de timers.
end
else -- Se o jogador que executou o comando não tem nenhuma solicitação na lista, então:
outputChatBox("Você não tem nenhuma solicitação de skin para aceitar ou recusar.", thePlayer)
end
end
addCommandHandler("aceitarskin", changeSkin)
addCommandHandler("recusarskin", changeSkin)