How can I make the remote event fire only when the mouse.Target is another player?

  1. What do you want to achieve? A remote event that fires when the mouse is hovering over a player (the characters have 3d clothing that are meshes stored inside of accessories)

  2. What is the issue? the script fires when the player click e, regardless of it hovering over a player of not

  3. What solutions have you tried so far? none-

here’s the script:

-- services, variables, and functions
local UserInputSerice = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()

local debounce = false

local WandaChoke = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaChoke")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://8342252625"

local anim = character.Humanoid:LoadAnimation(Animation)

local leftGlow = character.LeftHand.Attachment
local rightGlow = character.RightHand.Attachment

local function toggle(value)
	leftGlow.a.Enabled = value
	rightGlow.a.Enabled = value
end

-- code
UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		if mouse.Target:IsA("Accessory") or mouse.Target:IsA("BasePart") then -- i've written this part yet it doesn't work and still fires whenever
			debounce = true

			toggle(true)
			anim:Play()
			WandaChoke:FireServer(mouse.Target)

			task.wait(5)
			debounce = false
			toggle(false)
		
		end
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

Try this:

-- services, variables, and functions
local UserInputSerice = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()

local debounce = false

local WandaChoke = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaChoke")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://8342252625"

local anim = character.Humanoid:LoadAnimation(Animation)

local leftGlow = character.LeftHand.Attachment
local rightGlow = character.RightHand.Attachment

local function toggle(value)
	leftGlow.a.Enabled = value
	rightGlow.a.Enabled = value
end

-- code
UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
		if mouse.Target.Parent:FindFirstChild("Humanoid") or mouse.Target.Parent.Parent:FindFirstChild("Humanoid") then 
			debounce = true

			toggle(true)
			anim:Play()
			WandaChoke:FireServer(mouse.Target)

			task.wait(5)
			debounce = false
			toggle(false)

		end
	end
end)

Also, I didn’t change the InputService variable name but you should probably name it correctly so you don’t get confused later.

Unfortunate this didn’t work since the player has a layer of clothing on top of it (meshes) that’s why I checked for accessories because it’s pretty much all over the player’s body.

here’s what the character looks like if it helps:

-- services, variables, and functions
local UserInputSerice = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()

local debounce = false

local WandaChoke = ReplicatedStorage.WandaRemotes:FindFirstChild("WandaChoke")

local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://8342252625"

local anim = character.Humanoid:LoadAnimation(Animation)

local leftGlow = character.LeftHand.Attachment
local rightGlow = character.RightHand.Attachment

local function toggle(value)
	leftGlow.a.Enabled = value
	rightGlow.a.Enabled = value
end

-- code
UserInputSerice.InputBegan:Connect(function(input, gameprocessed)
	if gameprocessed then return end
	if input.KeyCode == Enum.KeyCode.E and debounce == false then
         
        if mouse.Target.Parent:FindFirstChild("Humanoid") or mouse.Target.Parent.Parent:FindFirstChild("Humanoid") then
			debounce = true

			toggle(true)
			anim:Play()
			WandaChoke:FireServer(mouse.Target)

			task.wait(5)
			debounce = false
			toggle(false)
		
		end
	end
end)

Just edited my code, try it again.

1 Like

this worked properly, thank you so much!

No problem, have a good day!

30