I can't use Touched event properly

Hey. There is a square locked on players mouse and it should make red every npc when touched. But it’s not working as expected. Sorry for my english but i will add a video too. Is there any alternative to touched event or am i doing something wrong ?

Here is my code :

RunService.RenderStepped:Connect(function(step)
	squareReal.Position = Vector3.new(mouse.Hit.p.X,0,mouse.Hit.p.Z)
	squareVisual.Position = Vector3.new(mouse.Hit.p.X,0,mouse.Hit.p.Z)
end

squareReal.Touched:Connect(function(part)
	if part.Parent.Parent.Name == "NPCs" then
		if part:IsA("MeshPart") then
			part.TextureID = ""
		end
	end
end)

squareReal.TouchEnded:Connect(function(part)
	if part.Parent.Parent.Name == "NPCs" then
		if part:IsA("MeshPart") then
			part.TextureID = part.Texture.Value
		end
	end
end)

robloxapp-20201106-0109256.wmv (3.5 MB)

2 Likes

Lua is case-sensitive, so make sure your parent keywords are capitalized. Like so:

if part.Parent.Parent.Name == "NPCs" then
1 Like

You should probably use a debounce with Touched events since it seems the red colour change is turning off and on but won’t stay on one colour.

Ahh yeah it’s my bad but behaving is the same with upper or lower ‘p’ weird.

I have no idea how to use debounce on this situation but i will try it.

Also, why make 2 variables for the same thing?
squareReal and squareVisual are identical values so just use 1 variable for both.