Help with Mouse.Target error

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I wanna be able to reset and set new mouse.target(variable is target) without attempted to index nil with .touched error appearing

  1. What is the issue? Include screenshots / videos if possible!

I have a telekinesis script and I when I throw the object with 2 any other mouse.targets that ive already made will be thrown too. So I added a mouse.target = nil so the mouse.target would reset, but then after I pick up a second thing the error, attemped to index nil with .touched appears in the output.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I’ve tried looking up solutions on google chrome and on dev hub, they were either confusing, or didn’t work.

Server Script; If you need to see the Local Script you can ask

target.Touched:Connect(function(hit)
						if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= player.Name then 
							if isGoing == false then
								isGoing = true
								local eneH = hit.Parent.Humanoid

								local found
								for _, character in next, hitE do
									if character == eneH then
										-- some boolean toggle for match
										found = true
									end
								end

								if not found then
									table.insert(hitE, eneH) 
									end
									
									eneH:TakeDamage(30)

									wait(.5)
									for i, character in next, hitE do
										if character == eneH then
											table.remove(hitE, i)
										end
								end
								
							end
						end
					end)
					
				wait(.05)
				isGoing = false
				BV:Destroy()
					
					target = nil

				TKStart:FireClient(player)
			    end)
			end)
			
			
			TKEnd.OnServerEvent:Connect(function(player)
			Anim:Stop()
			HoldGrip:Destroy()
			attachment0:Destroy()
				BG:Destroy()
				BV0:Destroy()
				
				target = nil
				
			TKStart:FireClient(player)
		end)

To recap, I want to be able to fix the attempted to index nil with .touched error but still be able to reset mouse.target(variable is target) and set a new one.

Mouse.Target cannot be set to something, it can only be read.
Also it says “attempt to index nil with .touched” because you set Mouse.Target to nil, your telling it to watch something that doesn’t exist (nil = nothing)

1 Like

I believe he’s creating a variable named “target” where the value of the “Target” property of the mouse object is being stored and that variable is later being assigned nil not the “Target” property of the mouse object itself.

Not entirely sure what you’re trying to achieve with the provided script but here’s a script which damages the player’s character’s humanoid when the cursor is hovered over a “BasePart” instance they are touching.

local players = game:GetService("Players")
local player = players.LocalPlayer
local mouse = player:GetMouse()

mouse.Move:Connect(function()
	if mouse.Target then
		if mouse.Target:IsA("BasePart") then
			local targetModel = mouse.Target:FindFirstAncestorOfClass("Model")
			if targetModel then
				local targetHuman = targetModel:FindFirstChildOfClass("Humanoid")
				if targetHuman then
					return
				end
			end
			mouse.Target.Touched:Connect(function(hit)
				local hitModel = hit:FindFirstAncestorOfClass("Model")
				if hitModel then
					local hitHuman = hitModel:FindFirstChildOfClass("Humanoid")
					if hitHuman then
						hitHuman:TakeDamage(hitHuman.Health)
					end
				end
			end)
		end
	end
end)