TargetFilter is not working

TargetFilter is not working in my script. I guess it’s because of what I use :Clone() made in the same script, but I don’t know how to solve this problem.

I also have another problem. For some reason, UserInputService.InputEnded:Connect(function(Button) is triggered twice in one click (I checked using print() ).

Script:

local UserInputService = game:GetService("UserInputService")
Status = false

UserInputService.InputEnded:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		Status = true
		local object = game.ReplicatedStorage.Bed:Clone()
		object:PivotTo(game.Players.LocalPlayer:GetMouse().Hit)
		object.Parent = game.Workspace
		
		game.Players.LocalPlayer:GetMouse().Move:Connect(function()
			if Status == true then
				local Mouse = game.Players.LocalPlayer:GetMouse()
				Mouse.TargetFilter = object
				local MousePosition = game.Players.LocalPlayer:GetMouse().Hit
				
				object:PivotTo(MousePosition)
				object:PivotTo(CFrame.new(object:GetPivot().Position.X, object:GetPivot().Position.Y + 1, object:GetPivot().Position.Z))
				object:PivotTo(CFrame.new(object:GetPivot().Position) * CFrame.Angles(math.rad(0),math.rad(0),math.rad(0)))
			end
		end)
		
		UserInputService.InputEnded:Connect(function(Button2)
			if Button2.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
				object = nil
				Status = false
			end
		end)
	end	
end)

I would be very grateful for a solution to at least one of these problems.

Are you trying to create an object placement system? Also… Never nest events the same way you’re doing it because for every event that fires new connections get made

You should be using Raycasts, the Mouse API is very, very old and pales in comparison.

i think inputended runs twice because you make a second connection inside of the first inputended function. if you really wanna do that you atleast have to disconnect that connection, right? or you could structure your script a bit differently, perhaps making an elseif after the first if button.keycode ==…

I did an experiment and removed most of the script, but unfortunately it didn’t work.

local UserInputService = game:GetService("UserInputService")
Status = false

UserInputService.InputEnded:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		print("Print")
		Status = true
		local object = game.ReplicatedStorage.Bed:Clone()
		object:PivotTo(game.Players.LocalPlayer:GetMouse().Hit)
		object.Parent = game.Workspace
	end	
end)

Desktop Screenshot 2025.03.26 - 16.53.44.64

Yes, I’m trying to make an item placement system.

Thanks, I’ll try to use it. And what are the main advantages?

woah the code looks kinda messy lemme fix that (take note that i just changed the way ur code looks, not solving the problem, and also its because there’s two connections nested inside a connection which might result in many unnecessary connections being made)

local Players = game:GetService("Players")
local Player = Players.LocalPlayer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Bed = ReplicatedStorage:WaitForChild("Bed")

local UserInputService = game:GetService("UserInputService")
local Status = false

local Mouse = Player:GetMouse()
local object: Instance? = nil

UserInputService.InputEnded:Connect(function(Button)
	if Button.KeyCode == Enum.KeyCode.E and Status == false then
		Status = true
		
		object = Bed:Clone()
		object:PivotTo(Mouse.Hit)
		object.Parent = workspace
	elseif Button.UserInputType == Enum.UserInputType.MouseButton1 and Status == true then
		Status = false
		object = nil
	end
end)

Mouse.Move:Connect(function()
	if Status == true and object ~= nil then
		Mouse.TargetFilter = object
		local MousePosition = Mouse.Hit
		
		object:PivotTo(MousePosition)
		object:PivotTo(CFrame.new(object:GetPivot().Position.X, object:GetPivot().Position.Y + 1, object:GetPivot().Position.Z))
		object:PivotTo(CFrame.new(object:GetPivot().Position) * CFrame.Angles(0, 0, 0))
	end
end)
1 Like

Thank you all for your help! The problem turned out to be that instead of LocalScript, I used a Script whose RunContext was changed to “local”.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.