Hello! I’m almost finished with a football (soccer) game, but not every goal works. It’s probably because .Touched is not that reliable. What is the best alternative? Expect the part to be touched a lot.
u could try and use GetPartBoundsInBox or GetPartsInPart
Please provide an example of how I can use GetPartsInPart() to check if there is a certain part named ‘Ball’ in the goal (part) and if it is, it prints “Goal!”
i guess something like this
local Ball = game.Workspace:FindFirstChild("Ball")
local touchingParts = Ball:GetTouchingParts() --// this should return array of the parts that are touching this object.
if #touchingParts > 0 then
print("Goal!")
else
--// we are not touching part
end
end
As of right now, it will print “Goal!” if it’s being touched, but how to make it so that if ‘Ball’ touches script.Parent. Only then it prints goal?
Sorry if this takes too much of your time, but I don’t really understand this
since GetTouchingParts() returns arrays of the parts that are touching objects u could use the if statement and check the part name, or the parent of it and etc stuff
Yeah but I can’t find any tutorials, I was guessing you could explain it
You could also do that with touched
script.Parent.Touched:Connect(function(Hit)
if Hit.Name == "Ball" then
print("Ball has touched the goal")
end
end)
It does not count everytime it touches. Very unreliable
You could add an int variable for that
local Goals = 0
script.Parent.Touched:Connect(function(Hit)
if Hit.Name == "Ball" then
Goals = Goals + 1
print(Goals.." Goals")
end
end)
As I just said above, touched is unreliable and does not fire everytime the part has ben touched. I’m already using .Touched as of right now and I need an alternative
I could be some use helping explain how GetTouchingParts() could work.
GetTouchingParts() just like how @bura1414 had said, it would return an array of BaseParts that were in a part, as the name suggests.
Some Example code:
local parts = workspace.CoolPart1:GetTouchingParts()
print(parts)
You should be getting an expected output of an table which can be empty or filled with baseparts that were colliding with CoolPart1. iirc that it wont return any BaseParts if CoolPart1
isn’t collideable, but using overlap prams, so to fix this issue you can use overlap prams.
Some exmaple usage
local overlapPrams = OverlapPrams.new()
-- Mess around with its properties. Use the link above to see what properties to be used
local parts = workspace:GetPartsInPart(workspace.CoolPart1, overlapPrams)
This would give the same output but instead even if the part that you are trying to get the parts inside of is not collideable would be able to detect the parts overlaping it. (IF the parts overlaping are collideable)
TL;DR
Use overlap prams to check if parts are inside of an part
Now for your issue you can use this peice of code to help
local Goal = workspace:WaitForChild('Goal')
local Ball = workspace:WaitForChild('Ball')
local prams = OverlapPrams.new() -- We would create prams for the GetPartsInParts to follow
prams.FilterDescendantsInstances = { Ball } -- Tell it to white list the ball only, so anything else besides the ball will be ignored
prams.FilterType = Enum.RaycastFilterType.Whitelist -- This makes it so only things inside of FilterDescendantsInstances would be allowed to be a valid part that was overlapping. If you did blacklist, then any thing else but the ball would be valid instead
function check()
local parts = workspace:GetPartsInPart(Goal, prams)
if table.find(parts, Ball) then -- Check if the instance 'Ball' is inside the array of parts
-- Code if the ball was in the goal
end
end
while task.wait() do
check()
end
I’ll go test it out, I insert it into the part that triggers the goals as of right now?
I would guess so, and if you can, you can show me the script just to see if i understand what you wrote since im not the very best of explaining everything. just making it work lol
Old script:
script.Parent.Touched:Connect(function(hit)
if hit.Name == "Ball" then
hit.Anchored = true
game.Workspace.BluePoint:FireAllClients(hit)
workspace.Ball.Position = Vector3.new(10, 110.5, -3)
workspace.Ball.Position = Vector3.new(19, 3.799, 1.5)
resetRed(); resetBlue()
game.Workspace.BluePoints.Value = game.Workspace.BluePoints.Value + 1
hit.Anchored = false
hit.AssemblyLinearVelocity = Vector3.new(0, 0, 0)
hit.AssemblyAngularVelocity = Vector3.new(0, 0, 0)
end
end)
It does not know what “OverlapPrams” is. It puts a blue line under it.
blue underline? can you send a screenshot maybe?
When you hover it, does it say something? or nothing really. It could be that I may of misspelled it but im not sure.
It says: ‘Unknown Global: OverlapPrams’