I am making a script where a ball hits a part a particle goes off. My problem is the script is not detecting the ball when it touches the part.
The script works if I remove the the ball touching function but I want it to work with the ball touching function so it does not happen from the player touching the part
How do I fix this
-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICES
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)
Do you have a localscript controlling the ball’s movement? It may be that the local script is not passing the ball’s position to the server, so the serverscript cannot register the ball is hitting anything.
-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICE
-- Get references to the ball and TweenService
local bladeBall = workspace.BladeBall
local TweenService = game:GetService("TweenService")
-- Function to move the ball to the target part smoothly
local function moveToPart(targetPart)
local ballPosition = bladeBall.Position
local targetPosition = targetPart.Position
-- Calculate the distance between the ball and the target
local distance = (targetPosition - ballPosition).Magnitude
-- Calculate the time it should take to reach the target based on distance
local timeToReach = distance / 20 -- Control the speed of the animation
-- Set up TweenInfo to control the animation
local tweenInfo = TweenInfo.new(
timeToReach,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out
)
-- Create and play the Tween for the ball's position
local tween = TweenService:Create(bladeBall, tweenInfo, {Position = targetPosition})
tween:Play()
tween.Completed:Wait()
end
-- Touched event for the ball
bladeBall.Touched:Connect(function(hit)
local character = hit.Parent
if character and character:FindFirstChild("Humanoid") then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
local team = player.Team
if team then
if team.Name == "Blue" then
moveToPart(workspace.OnangeHitBox)
elseif team.Name == "Orange" then
moveToPart(workspace.BlueHitBox)
end
end
end
end
end)
Have a look at this thread here. Your issue is that you’re using a tween to simulate the ball reaching its target, not the Roblox physics engine. So, the touched event will not fire. Your best bet is to use GetTouchingParts.
While probably not ideal, this is one possible alternative to replace your
ball.Touched:Connect()
function in your original code:
while wait(1) do
local function GetTouchingParts(part)
local connection = ball.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local results = GetTouchingParts(workspace.Part0)
print(#results) --Number of touching parts
for i, v in pairs(results) do
if v == partO then
print("Ball touched part0")
--Do stuff here, such as particle effects
elseif v == partB then
print("Ball touched partB")
--Do stuff here, such as particle effects
end
end
end
Ok my bad the code some what works. I don’t know how to explain what is happening
is there a way to fix it
aslo the code only works when I change like this
while wait(1) do
local function GetTouchingParts(part)
local connection = bladeBall.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local results = GetTouchingParts(workspace.OnangeHitBox)
print(#results) --Number of touching parts
print("Ball touched part0")
createParticleO()
resetBallPosition()
end
I prefer to fix this code and not us the other methods like RayCasting and Region3s because I don’t understand them
I think I can help you with your problem. The reason why your script is not detecting the ball touching the part is because you are using a tween to move the ball. Tweens do not simulate the Roblox physics engine, so the touched event will not fire when the ball reaches its target.
To fix this, you can use the GetTouchingParts() function instead. This function will return a list of all the parts that are currently touching the specified part. You can then check this list to see if the ball is touching the part you want it to be touching.
Here is a modified version of your script that uses GetTouchingParts() to detect the ball touching the part:
-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICE
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
while wait(1) do
local function GetTouchingParts(part)
local connection = ball.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local results = GetTouchingParts(workspace.OnangeHitBox)
for i, v in pairs(results) do
if v == partO then
createParticleO()
resetBallPosition()
elseif v == partB then
createParticleB()
resetBallPosition()
end
end
end
I have also changed the script to loop forever using a while loop. This is because the GetTouchingParts() function will only return a list of the parts that are currently touching the specified part. If the ball is not touching the part when the script checks, then the script will not do anything. By looping forever, the script will continuously check if the ball is touching the part until it is.
Nothing happened. I redid the script just renaming the variables and removed the waits on the createParticle functions
Also I looked more into it and if the ball goes to the blueHitBox first will the script still work
-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICE
local bladeBall = workspace.BladeBall
local onangeHitBox = workspace.OnangeHitBox
local blueHitBox = workspace.BlueHitBox
local bladeBallSpawn = workspace.BladeBallSpawn
local particleO = onangeHitBox.Attachment.ParticleEmitter
local particleB = blueHitBox.Attachment.ParticleEmitter
-- Function to reset the ball's position
local function resetBallPosition()
bladeBall.Transparency = 1
bladeBall.Highlight.OutlineTransparency = 1
bladeBall.CFrame = bladeBallSpawn.CFrame
wait(3)
bladeBall.Highlight.OutlineTransparency = 0
bladeBall.Transparency = 0
end
-- Function to make a particle effect for PartO
local function createParticleO()
particleO:Clear()
particleO:Emit(1) -- number of particles emitted
end
-- Function to make a particle effect for PartB
local function createParticleB()
particleB:Clear()
particleB:Emit(1) -- number of particles emitted
end
-- Ball touch events
while wait(1) do
local function GetTouchingParts(part)
local connection = bladeBall.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local results = GetTouchingParts(workspace.OnangeHitBox)
for i, v in pairs(results) do
if v == onangeHitBox then
createParticleO()
resetBallPosition()
elseif v == blueHitBox then
createParticleB()
resetBallPosition()
end
end
end
-- SERVER SCRIPT LOCATED IN SERVERSCRIPTSERVICE
-- Get references to the bladeball, hitboxes, bladeballspawn, and particles
local bladeBall = workspace.BladeBall
local onangeHitBox = workspace.OnangeHitBox
local blueHitBox = workspace.BlueHitBox
local bladeBallSpawn = workspace.BladeBallSpawn
local particleO = onangeHitBox.Attachment.ParticleEmitter
local particleB = blueHitBox.Attachment.ParticleEmitter
-- Function to reset the ball's position
local function resetBallPosition()
bladeBall.Transparency = 1
bladeBall.Highlight.OutlineTransparency = 1
bladeBall.CFrame = bladeBallSpawn.CFrame
wait(3)
bladeBall.Highlight.OutlineTransparency = 0
bladeBall.Transparency = 0
end
-- Function to make a particle effect for PartO
local function createParticleO()
particleO:Clear()
particleO:Emit(1) -- number of particles emitted
end
-- Function to make a particle effect for PartB
local function createParticleB()
particleB:Clear()
particleB:Emit(1) -- number of particles emitted
end
-- Ball touch events
while wait(1) do
local function GetTouchingParts(part)
local connection = bladeBall.Touched:Connect(function() end)
local results = part:GetTouchingParts()
connection:Disconnect()
return results
end
local resultsO = GetTouchingParts(onangeHitBox)
local resultsB = GetTouchingParts(blueHitBox)
for i, v in pairs(resultsO) do
if v == bladeBall then
createParticleO()
resetBallPosition()
end
end
for i, v in pairs(resultsB) do
if v == bladeBall then
createParticleB()
resetBallPosition()
end
end
end