Help With Touch Event

I am trying to make a script where if a ball hits a part a particle goes off. I have to parts and the script is located in ServerScriptServices

The problem is both parts’ particles go off

How do I fix this

-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICES
-- Get references to the ball and particle effects
local ball = workspace.Ball
local partO = workspace.PartO
local partB = workspace.PartB
local ballSpawn = workspace.BallSpawn
local particle1 = partO.Attachment.ParticleEmitter
local particle2 = partB.Attachment.ParticleEmitter

function onTouchO()
	if (ball ~=nil) then
		-- Turn on the particle effect for PartO
		particle1:Clear()
		particle1:Emit(10)
		wait(1)
		-- Disable the ball's visibility for Part)
		ball.Position = ballSpawn.Position
		ball.Transparency = 1
		ball.Highlight.OutlineTransparency = 1
		task.wait(3)
		ball.Transparency = 0
		ball.Highlight.OutlineTransparency = 0
	end
end

function onTouchB()
	if (ball ~=nil) then
		-- Turn on the particle effect for PartB
		particle2:Clear()
		particle2:Emit(10)
		wait(1)
		-- Disable the ball's visibility for Part)
		ball.Transparency = 1
		ball.Highlight.OutlineTransparency = 1
	end
end

ball.Touched:Connect(onTouchO)
ball.Touched:Connect(onTouchB)
5 Likes

It looks like you’ve set TWO listeners to the ball.touched event. You only need one. In the touch event function you need to discern which part was touched… Then fire the particles for that part only.

local function onTouch(touchedPart)

    if(touchedPart.Name == "Part0") then
        --Do particles for Part0
    end
etc...
end
3 Likes

It’s not detecting when the ball touches the part. If the player touches the part it works fine but it does not work when the ball touches it

3 Likes

Are both the ball and this part anchored?

1 Like

Yes there are both anchored-------------

2 Likes

It appears that your current configuration triggers particle emission when the ball makes contact with any object. This is because you have set up two separate “touched” events for the ball. If you intend for these events to occur one at a time, you might consider using a single “touched” event along with conditional statements to determine the type of object the ball is interacting with.

local onTouch(hit)
 if ball and hit == partO then
  --part0 script here
 elseif ball and hit == part1 then
  --part1 script here
 end
end

ball.Touched:Connect(onTouch)
1 Like

Screenshot 2023-10-15 at 11.44.57
This is happening and i don’t know how to fix it

1 Like

sorry that’s my mistake I forgot to rename the function within the touched event, just change onTouch0 to onTouch

2 Likes

I did change that but I need to also change it to

local function onTouch(hit)

also when the ball hits that part nothing happens

1 Like

Let me see the new script please, also I’m sorry for the typo’s I’m on mobile as of now.

1 Like

no problem

-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICES
-- Get references to the ball and particle effects
local ball = workspace.Ball
local partO = workspace.PartO
local partB = workspace.PartB
local ballSpawn = workspace.BallSpawn
local particle1 = partO.Attachment.ParticleEmitter
local particle2 = partB.Attachment.ParticleEmitter

local function onTouch(hit)
if ball and hit == partO then
	-- Turn on the particle effect for PartO
	particle1:Clear()
	particle1:Emit(10)
	wait(1)
	-- Disable the ball's visibility for Part)
	ball.Position = ballSpawn.Position
	ball.Transparency = 1
	ball.Highlight.OutlineTransparency = 1
	task.wait(3)
	ball.Transparency = 0
	ball.Highlight.OutlineTransparency = 0
elseif ball and hit == partB then
	particle2:Clear()
	particle2:Emit(10)
	wait(1)
	-- Disable the ball's visibility for Part)
	ball.Position = ballSpawn.Position
	ball.Transparency = 1
	ball.Highlight.OutlineTransparency = 1
	task.wait(3)
	ball.Transparency = 0
	ball.Highlight.OutlineTransparency = 0
end
end


ball.Touched:Connect(onTouch)
2 Likes

Before the if statements, can you add this print to make sure it’s being called and to verify the values of hit and other variables:
print(“Touched:”, hit, “part0:”, part0, “part1:”, part1)

1 Like

If this is what you want --correct me if not-- only Touched hit comes out in the output

-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICES
-- Get references to the ball and particle effects
local ball = workspace.Ball
local partO = workspace.PartO
local partB = workspace.PartB
local ballSpawn = workspace.BallSpawn
local particle1 = partO.Attachment.ParticleEmitter
local particle2 = partB.Attachment.ParticleEmitter

local function onTouch(hit)
	print("Touched hit")
	if ball and hit == partO then
		print("Touched partO")
	-- Turn on the particle effect for PartO
	particle1:Clear()
	particle1:Emit(10)
	wait(1)
	-- Disable the ball's visibility for Part)
	ball.Position = ballSpawn.Position
	ball.Transparency = 1
	ball.Highlight.OutlineTransparency = 1
	task.wait(3)
	ball.Transparency = 0
	ball.Highlight.OutlineTransparency = 0
	elseif ball and hit == partB then
		print("Touched partB")
	particle2:Clear()
	particle2:Emit(10)
	wait(1)
	-- Disable the ball's visibility for Part)
	ball.Position = ballSpawn.Position
	ball.Transparency = 1
	ball.Highlight.OutlineTransparency = 1
	task.wait(3)
	ball.Transparency = 0
	ball.Highlight.OutlineTransparency = 0
end
end


ball.Touched:Connect(onTouch)
2 Likes

No, just replace print(“Touched hit”) with the print I gave you

1 Like

Nothing happen, no outputs or anything. Also to make things easer I made a different script

But sill when the ball hits the part nothing happened

local ball = workspace.Ball
local partO = workspace.PartO
local partB = workspace.PartB
local ballSpawn = workspace.BallSpawn
local particleO = partO.Attachment.ParticleEmitter
local particleB = partB.Attachment.ParticleEmitter

-- Function to reset the ball's position
local function resetBallPosition()
	ball.Transparency = 1
	ball.Highlight.OutlineTransparency = 1
	ball.CFrame = ballSpawn.CFrame
end

-- Function to make a particle effect for PartO
local function createParticleO()
	particleO:Clear()
	particleO:Emit(1) -- number of particles emitted
	wait(1)
end

-- Function to make a particle effect for PartB
local function createParticleB()
	particleB:Clear()
	particleB:Emit(1) -- number of particles emitted
	wait(1)
end

-- Ball touch events
ball.Touched:Connect(function(otherPart)
	if otherPart == partO then
		createParticleO()
		resetBallPosition()
	elseif otherPart == partB then
		createParticleB()
		resetBallPosition()
	end
end)
2 Likes

I’m glad you decided to organize the script with functions, easier to read.
Now add this print statement to verify the values of hit and other variables

ball.Touched:Connect(function(otherPart)
print(“Touched:”, otherPart, “part0:”, part0, “part1:”, partB)
	if otherPart == partO then
		createParticleO()
		resetBallPosition()
	elseif otherPart == partB then
		createParticleB()
		resetBallPosition()
	end
end)

It prints but nothing happens----------

AFAIK two anchored parts will not fire the touched event.

I recommend repeated raycasting for hit detection (you could do this on the client if you’re worried about server load).

1 Like

I used a set up where the parts are not anchored and that did not work also I want it to be server side

Could you clarify what exactly you want here? Do you want the parts to react differently if hit by the ball, or do you want something else?