Hello everyone, i have a problem with the touched event : ( Help me
Here the situation :
It’s only working on my avatar but not the part (brown)
Here the code :
local Part = script.Parent
local CollectValue = Part.Collecting
if Hit.Parent.Name == "PottingSoil" then
local Ground = Hit.Parent.Ground
if Hit.Parent.Used.Value == false then
Hit.Parent.Used.Value = true
Ground.Transparency = 0.5
Hit.Parent.PlantName.Value = Part.PlantToPlant.Value
Assuming that both parts are Anchored, the Touched event will never fire. This is just a caveat of using the Touched method. I don’t exactly know why this is the case, but if I had to guess, it probably has something to do with the fact that Touched relies on a “physics context” (e.g. a part falling on another) and the Anchored property nullifies all “physics contexts”. Anyway, in summary, if both parts have their Anchored property to true, Touched will not fire. You can even prove this to yourself by making the Anchored property of one of the parts to false and see if it prints.
If my diagnosis is correct, here a couple of solutions that might work off the top of my head:
Raycasting: You could try raycasting (in the same way you’ve probably raycasted to create the ghost part placement system) and check to see if the cast is hitting the brown part. The only problem with this is when you move your mouse right next to the brown part (but not on it), the ghost part would technically be hitting the part (depending how you implemented the system) but the raycast wouldn’t be. Overall, however, this solution would probably work in most scenarios, depending on what exactly you’re trying to achieve.
Query Selectors: This solution is similar to raycasting, however, a query selector (see link below, they’re now not referred to as query selectors anymore unbeknownst to me) kind of acts as a 3D cast, where you can detect parts in a box or defined bounds. You could create a query selector with the same bounds as the ghost part, and if the brown part is inside of it, then you know the two are “touching”. In theory, this system should work exactly how you want it to. However, one major drawback with this approach is that query selectors are notoriously performant heavy. Be mindful of where you use these and how often you create one.
Those are just two solutions that I could see working. There are probably better solutions out there, and since the two that I’ve presented do have some flaws, I’d do some research into alternatives.
In case you’re interested in my solutions, here are some links to articles related to the topics I discussed.