Mouse.target wont detect a player

So I am making a tool to push players with this is the code:
script.Parent.Equipped:Connect(function(Mouse)
local player = game.Players.LocalPlayer
local plrmouse = player:GetMouse()
Mouse.Button1Down:Connect(function()
print(“Broom”)
for i, v in pairs(game.Players:GetChildren()) do
if plrmouse.Target == player then
print(“You cant use broom on yourself”)
elseif plrmouse.Target == v then
print(“Model”)
end
end
end)
end)

script.Parent.Unequipped:Connect(function()
print(“No broom :(”)
end)

.Target will target the player Body Parts and not the player Instance. You will need this additional function to get the player from mouse.Target.

local Players = game:GetService("Players")
 
local playerHit = Players:GetPlayerFromCharacter(plrmouse.Target.Parent)

if playerHit  then
--do stuff
end
2 Likes

Thx for helping :smiley: :smiley: :smiley: :smiley: :smiley:

script.Parent.Equipped:Connect(function(Mouse)
local player = game.Players.LocalPlayer
local Players = game:GetService(“Players”)
local plrmouse = player:GetMouse()
Mouse.Button1Down:Connect(function()
local playerhit = Players:GetPlayerFromCharacter(plrmouse.Target.Parent)

	if playerhit then
		print(playerhit.Name.." has been pushed")
		local bv = Instance.new("BodyVelocity", playerhit)
		bv.Velocity = player.Character.HumanoidRootPart.CFrame.lookVector * 5
		wait(5)
		bv:Destroy()
	end
end)

end)

script.Parent.Unequipped:Connect(function()
print(“No broom :(”)
end)
It doesn’t work that well

It doesn’t print the player has been pushed most of the time

You may need to check if playerhit.Parent.Parent is a player in case you hit an accessory I think. I’m not the best scripter and that may not fix it but maybe try it. If that doesn’t work then maybe spawn a medium-sized brick at the target point and check if any parts that are touching that part are part of a character. I know that is a real messy way to do it but I think it might work. Make sure to try my first suggestion first though.