Creating a particleeffect anchored to player?

Title may seem vague but I don’t know how else I would word it.

I currently need help with creating a script that detects a player within a region. When detected it will create a part with a particle effect a distance away from the player while they move. The particle in question is supposed to resemble a snow storm flying at the player.

Just note that I am pretty much a beginner.

Here is a script I have tried to make, and as you can see it won’t work.

local region = script.Parent
   
    local function onPartTouched(otherPart)
        local partParent = otherPart.parent
        local humanoidroot = partParent:FindFirstChildWhichIsA("HumanoidRootPart")
        if humanoidroot then
           local ParticleMaker = Instance.new("Part")
            ParticleMaker.Parent = humanoidroot 
            local weld = Instance.new("Weld")
           weld.Part0 = ParticleMaker
          weld.Part1 = humanoidroot
         ParticleMaker.CFrame = CFrame.new(ParticleMaker.Position) + Vector3.new(0, 0, -50)
           
          
     end
    end
   
    region.Touched:Connect(onPartTouched)
1 Like

Your problem is here:

Try FindFirstChild. The method FindFirstChildWhichIsA is the equivalent of looping through a GetChildren table and running IsA on each object, returning the first match. It’s supposed to find an object of a base class, rather than a name.

You should also be using the new WeldConstraint rather than the original weld because it’s easier to use, without having to mess with C0 or C1 properties. I couldn’t get the object to float in front of me with the original weld either.

I can see this line

ParticleMaker.CFrame = CFrame.new(ParticleMaker.Position) + Vector3.new(0, 0, -50)

is probably not what you meant to do. This code just puts the particle maker at 0, 0, -50 because CFrame.new(ParticleMaker.Position) is initially at 0, 0, 0. I think you meant to put it 50 studs in front of the torso.

I suggest you add a debounce to this Touched listener as well. If you know Touched well, then you know how spammy it can be.

Here is the code with all of these issues fixed. It uses a WeldConstraint, spawns the part 50 studs in front of you, and has a 3 second debounce for each character.

local region = script.Parent
local Debounce = {}

local function onPartTouched(otherPart)
	local humanoidroot = otherPart.parent:FindFirstChild("HumanoidRootPart")
	if humanoidroot and not Debounce[humanoidroot] then
		Debounce[humanoidroot] = true
		local ParticleMaker = Instance.new("Part")
		local weld = Instance.new("WeldConstraint")
		weld.Part0 = ParticleMaker
		weld.Part1 = humanoidroot
		weld.Parent = ParticleMaker
		ParticleMaker.CFrame = humanoidroot.CFrame + (humanoidroot.CFrame.LookVector * 50)
		ParticleMaker.Parent = workspace 
		wait(3)
		Debounce[humanoidroot] = nil
	end
end

region.Touched:Connect(onPartTouched)

One issue remains, however… https://gyazo.com/3f0b7b430b7546b3eda7956632b29df4

Welding anything to the character, or even just having unwelded parts descend from the character, has always caused issues like this for me with varying degrees of effect. I don’t really know how to solve this particular issue. If welds do this and there’s nothing that can fix it, the only solution I can think of would be to just have an anchored part constantly refresh its position to be 50 studs in front of the torso.

Seems to work, thanks.

Though as you said, there is still a problem.
I think I’ve decided to move it a distance from the camera position instead. I believe that would be better, but I have no idea how to position things from the camera of the local player.

Well, we can’t use a weld because the camera isn’t a part. But I’ve done exact thing before. In this gif you can see the arrow is an actual 3D object always in front of the camera.
https://gyazo.com/98e49d39b3e5fa85766899374d2213c4
All that’s going on is something like this:

workspace.CurrentCamera:GetPropertyChangedSignal("CFrame"):Connect(
    function()
        local CameraCFrame = workspace.CurrentCamera.CFrame
        Object.CFrame = CameraCFrame + (CameraCFrame.LookVector * 10) + (CameraCFrame.UpVector * 5)
        -- Object would just be whatever part you want to be in front of the camera
    end
)