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)
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
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)
-- 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)
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)
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)
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)
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)