How do I prevent mouse.Target on a player?

Hello, I have been working on a little project laterly and I made a gravity gun, but now the issue is you can pick up other players which is not what I want. I do not want to search for humanoid because I want you to be able to pick up NPC’s. Is there another solution? The one I use now some how bypasses and still lets you pick up the other player. Thanks.

local isPlayer = game.Players:GetPlayerFromCharacter(heldObject.Parent)
if not isPlayer then

If you don’t want to pick up any player you could just add something like this:

function IsPlayer(Char) -- Returns True Or False
   local player = game.Players:GetPlayerFromCharacter(Char)

   if player then
      return true
   end


 return false
end

--Usage -- 
if not IsPlayer(char) then
 return
end
----------

Or you could try something like this though it would have more lag and does the same thing.


function IsPlayer(Char) -- Returns True Or False
   local player = nil

   for i,p in pairs(game.Players:GetChildren()) do 
     if p.Name == Char.Name then
         local PlrChar = p.Character or p.CharacterAdded:Wait()
         if PlrChar == Char then
            player = p
         end
      end
   end

   if player then
      return true
   end

 return false
end

--Usage -- 
if not IsPlayer(char) then
 return
end

Though this does the same thing so I recommend the first way.

1 Like

Use Mouse.TargetFilter

This still dosent not work still picks up player

but how would I use this, we still dont know if its a player or not without humanoid?

For example:
add this to ServerScriptService

Players = game.Players

folder = Instance.new("Folder",workspace)
folder.Name = "CharacterFolder"

Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(char)
        wait(2) --Wait for character to load
        char.Parent = folder
    end)
end)

Now, you can do Mouse.TargetFilter = worksapce.CharacterFolder, so players will be ignored.

2 Likes

Thanks I think it works, although its not working in studio since for some reason the players arent being added to the folder but looks like its going to work. Thanks

I tested it, and it wasn’t working becouse character didn’t load. You must add wait(2) before changing parent

1 Like

but how I can I do this faster? Since another player could weld them to some thing while it waits.

never mind I just did a wait(player.Character)

You can also use Player.CharacterAppearanceLoaded instead of CharacterAdded

1 Like

So I tried out my other weld gun and it seems not to work lol, then like the gravity gun. I still have mouse.TargetFilter = game.Workspace:WaitForChild(“Players”) but im still able to weld the player to an object.

If it helps im just setting to variables to mouse.Target

Its working for me

Mouse Target Test.rbxl (21.7 KB)
(Use 2 players simulation, Player1 has enabled FilterTarget, Player2 not)

Also mouse.Target always ignores character of local player