.Touched not firing

Hi devs!

I wanna make a meteor rain, where it clones meteors to the sky and tweens them to the bottom of the world, where whenever the meteor touches a part, it gets destroyed. However, the .Touched event doesn’t fire whatsoever.

Before any asks, all parts have CanTouch set to true, all parts are in workspace, and the script is a ServerScript located in ServerScriptStorage.

Thanks in advance!

local destroymeteor = false
local multiplier = 1

for i = 1, 6 do
	multiplier = multiplier + 0.3
	local randomx = math.random(-257.4, -71.5)
	local randomz = math.random(-161, 25)

	local clone2 = workspace.LightningAlert:Clone()
	clone2.Parent = workspace clone2.Position = Vector3.new(randomx, -44.709, randomz)
	clone2.PointLight.Enabled = true
	clone2.Transparency = 0
	clone2.Transparency = 1
	clone2.Name = "indicator"
	local tween = game:GetService("TweenService"):Create(clone2, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),{Transparency = 0}):Play()

	local clone3 = workspace.Meteor:Clone()
	clone3.Parent = workspace
	clone3.Position = Vector3.new(randomx, 200, randomz)
	clone3.Transparency = 0
	clone3.Fire.Enabled = true

	local tween = game:GetService("TweenService"):Create(clone3, TweenInfo.new(4.3 / multiplier, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut),{Position = Vector3.new(randomx, -82, randomz)}):Play()

	repeat wait(0.1)
		clone3.Touched:Connect(function(p)
			print("part touched meteor")
			destroymeteor = true
			if p.Parent:FindFirstChildOfClass("Humanoid") and p.Name == "HumanoidRootPart" then
				p.Parent.Humanoid.Health = p.Parent.Humanoid.Health - 55
			end
		end)
	until destroymeteor == true

	destroymeteor = false
	game.SoundService.Explosion:Play()
	game.ReplicatedStorage.Remotes.Camera.Explosion:FireAllClients()

	local explosion = Instance.new("Explosion")
	explosion.Parent = workspace
	explosion.Position = Vector3.new(randomx, clone3.Position.Y, randomz)

	clone3:Destroy()
	clone2:Destroy()

	wait(1)
end

multiplier = 1 
script.Enabled = false

Edit: forgot to mention, the output never prints any error nor the “part touched meteor” from the .Touched function.

3 Likes

There gonna be a memory leak because you keep repeating the .Touched function in the repeat until loop

3 Likes

The repeat loop is useless. Write it like this instead:

clone3.Touched:Connect(function(p)
	print("part touched meteor")
	if destroymeteor == false then
		destroymeteor = true
		if p.Parent:FindFirstChildOfClass("Humanoid") and p.Name == "HumanoidRootPart" then
			p.Parent.Humanoid.Health = p.Parent.Humanoid.Health - 55
		end
	end
end)
2 Likes

This loop will never exit because you are not changing the value of destroymeteor inside the loop.
You can remove the repeat loop and place the Touched event connection outside the loop🥰

local destroymeteor = false
local multiplier = 1

for i = 1, 6 do
    multiplier = multiplier + 0.3
    local randomx = math.random(-257.4, -71.5)
    local randomz = math.random(-161, 25)

    local clone2 = workspace.LightningAlert:Clone()
    clone2.Parent = workspace
    clone2.Position = Vector3.new(randomx, -44.709, randomz)
    clone2.PointLight.Enabled = true
    clone2.Transparency = 0
    clone2.Transparency = 1
    clone2.Name = "indicator"
    local tween = game:GetService("TweenService"):Create(clone2, TweenInfo.new(0.8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Transparency = 0}):Play()

    local clone3 = workspace.Meteor:Clone()
    clone3.Parent = workspace
    clone3.Position = Vector3.new(randomx, 200, randomz)
    clone3.Transparency = 0
    clone3.Fire.Enabled = true

    clone3.Touched:Connect(function(p)
        print("part touched meteor")
        destroymeteor = true
        if p.Parent:FindFirstChildOfClass("Humanoid") and p.Name == "HumanoidRootPart" then
            p.Parent.Humanoid.Health = p.Parent.Humanoid.Health - 55
        end
    end)

    local tween = game:GetService("TweenService"):Create(clone3, TweenInfo.new(4.3 / multiplier, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = Vector3.new(randomx, -82, randomz)}):Play()

    repeat wait(0.1) until destroymeteor == true

    destroymeteor = false
    game.SoundService.Explosion:Play()
    game.ReplicatedStorage.Remotes.Camera.Explosion:FireAllClients()

    local explosion = Instance.new("Explosion")
    explosion.Parent = workspace
    explosion.Position = Vector3.new(randomx, clone3.Position.Y, randomz)

    clone3:Destroy()
    clone2:Destroy()

    wait(1)
end

multiplier = 1 
script.Enabled = false
2 Likes

Keep in mind that Touched will not fire if the object is not physically simulated, aka, anchored.

1 Like

Thanks for bringing that to my attention! I’ll try it out as soon as I can and tell you if it works.

I hope you can fix the issue soon😇

1 Like

I thought that tweening an object simulates it anyway, since it moves, right…?
That doesn’t make any sense, but after I set it to unanchored, it works now. Thanks for the reply!
Also thanks for @tutab4 for the improved code, I fixed the lagspike now. :slight_smile:

1 Like

Did Roblox changed it?
I always remember old tycoons VIP doors are anchored, the Touched event shouldn’t fire if “CanTouch” property is off.

It’s pretty odd if Anchored parts don’t fire Touched anymore…
Roblox changes on their physics pretty weird/haywire this time, I’m trying to understand a bug on AlignPosition right now too :skull:

1 Like

Oh, the part must be unanchored because if anchored == true it won’t let your script to work. Also I’m happy that you fixed the lagspike.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.