luaroblox

Avatar inspect script always returns "Instance"


I'm trying to make a game that allows players to inspect any other players' avatar by sitting in a specific seat and chatting their UserId or username, but when a user enters a username, the function always returns Instance, which is annoying. However, the UserId works. Here's my code:

-- client-side script
local Players=game:GetService("Players")
local Player=Players.LocalPlayer
local GuiService=game:GetService("GuiService")
local Rep=game:GetService("ReplicatedStorage")
repeat wait(0.1) until Player.Character
local h=Player.Character:WaitForChild("Humanoid")
GuiService:SetInspectMenuEnabled(false)
Player.Chatted:Connect(function(m)
    if h.Sit then
        if #string.split(m," ")==1 then
            if tonumber(m)~=nil then
                if h.SeatPart.Name=="AvatarInspectMenu" then
                    local id=tonumber(m)
                    GuiService:InspectPlayerFromUserId(id)
                end
            else
                if h.SeatPart.Name=="AvatarInspectMenu" then
                    local id=Rep.Idify:InvokeServer(m)
                    GuiService:InspectPlayerFromUserId(id)
                end
            end
        end
    end
end)

--server-side script
local cache={}
game:GetService("ReplicatedStorage").Idify.OnServerInvoke=function(n)
    if cache[n] then return cache[n] end
    local player=game.Players:FindFirstChild(n)
    if player then
        cache[n]=player.UserId
        return player.UserId
    end
    local id
    pcall(function ()
        id = game.Players:GetUserIdFromNameAsync(n)
    end)
    cache[n] = id
    return id
end

The client-side script works well, and I get an Avatar Inspect Menu for ROBLOX when I chat 1 in the chair, but when I chat ROBLOX in the chair, I get an Avatar Inspect Menu for Instance, or nil. Is there any possible way to fix this error?


Solution

  • The first argument passed to OnServerInvoke is the player that invoked the function, see OnServerInvoke

    Since you are not interested in that, you can just ignore it by using a dummy underscore, like that:

    game:GetService("ReplicatedStorage").Idify.OnServerInvoke=function(_, n)