Touch Event Not Triggering

  1. What I Want to Achieve: I want a ball in my game to change the color of any other part it touches. The ball is supposed to move towards the mouse position and then apply a color change upon contact with other parts.

  2. Issue: The touch event associated with the ball isn’t working. Despite the ball moving and contacting other parts, no color change occurs, and the expected print statement in the console does not appear…

  3. Solutions I’ve Tried:

  • I checked the Roblox Developer Hub for issues similar to mine but didn’t find anything directly applicable.

  • I confirmed that the part names are correct and that the touched parts are indeed Roblox “Part” objects.

  • I have also ensured that there are no issues with the script syntax.

Additional Details:
I suspect the issue might be related to how I’m connecting the touch event or perhaps the conditions under which the event is supposed to trigger. Any insights or suggestions would be greatly appreciated!

local Mouse = game.Players.LocalPlayer:GetMouse()
local ball = game.Workspace.Parth

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local function moveBall()
	local mouseHit = Mouse.Hit
	local direction = (mouseHit.Position - game.Workspace.CurrentCamera.CFrame.Position).unit
	local distance = 30
	local targetPosition = game.Workspace.CurrentCamera.CFrame.Position + direction * distance

	local tweenInfo = TweenInfo.new(
		0.1,            -- Time
		Enum.EasingStyle.Linear,    -- Easing style
		Enum.EasingDirection.InOut, -- Easing direction
		0,              -- Repeat count (zero = no repeat)
		false,          -- Reverses after repeating
		0               -- Delay
	)

	local tween = TweenService:Create(ball, tweenInfo, {Position = targetPosition})
	tween:Play()
end

Mouse.Move:Connect(moveBall)

RunService.Heartbeat:Connect(function()
	moveBall()
end)

ball.Touched:Connect(function(otherPart)
	if otherPart:IsA("Part") then
		otherPart.BrickColor = BrickColor.random()
		print("otherPart" .. otherPart.Name)
	end
end)

1 Like

I wouldn’t used .Touched most of the time, as for stuff like this, it’s really unreliable. I would recommend using :GetPartsInPart() instead. It get’s every physically touching part in a list, and you can do whatever from there. Here’s a script that I think would help:

local FilterObjects = {} -- Objects you want to not be affected
local MaxObjectsAllowed = 0 -- How many objects are allowed to be affected (0 means infinite)

local Params = OverlapParams.new() -- Just some limits (don't need to change anything)
Params.CollisionGroup = "Default"
Params.FilterDescendantsInstances = FilterObjects
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.MaxParts = MaxObjectsAllowed

while wait() do
	local ObjectsInPart = game.Workspace:GetPartsInPart(script.Parent, Params)
	
	for i, part in pairs(ObjectsInPart) do
		part.BrickColor = BrickColor.random()
		print("Part touched name is " .. part.Name)
	end
end
1 Like


It works thank you

1 Like

I agree; and to clarify / add onto this for OP, the Touched event only fires from physics-based interactions.

As a result, when the TweenService code updates the Position of the ball, it would not fire the Touched event upon being moved to the new Position unless the object it starts to interact with happens to physically move (without the Position / CFrame of the second object being updated via a script).

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