Why is the function spammed?

I’m trying to use a ray cast to detect if the piano is touched or not, but the issue with it is for some reason it is spammed for an indefinite amount of time…

This is the code that makes the piano explode, and it seems like the explode() function keeps getting fired even though activated = true

local raycastResult
local activated = false
function raycastDown(bombClone, user)
	if not activated then
		local rayParams = RaycastParams.new()
		rayParams.FilterType = Enum.RaycastFilterType.Exclude
		rayParams.FilterDescendantsInstances = {Piano}

		raycastResult = workspace:Raycast(Piano.Position, Vector3.new(0, -4, 0), rayParams)

		if raycastResult then
			local partsInRadius = Piano:GetTouchingParts()
			for i, v in pairs(partsInRadius) do
				if game:GetService("Players"):GetPlayerFromCharacter(v) then
					local player = game:GetService("Players"):GetPlayerFromCharacter(v)

					if player and player.Team ~= user.Team then
						raycastResult.Instance.Parent:FindFirstChildOfClass("Humanoid"):TakeDamage(35)
						explode(bombClone, user)

						activated = true
					end
				end
			end

			if activated == false then -- if the connection is still true and no players have been hit, then just explode the piano.
				explode(bombClone, user)

				activated = true
			end
		end
	end
end

local connection
ActivateBombRemote.OnServerEvent:Connect(function(user, pianoDropCFrame)
	local bombClone = Piano:Clone()
	bombClone.Parent = game.Workspace
	bombClone.CanCollide = true
	bombClone.CFrame = pianoDropCFrame * CFrame.new(0, 100, 0)
	bombClone.Anchored = false
	bombClone.Transparency = 0
	
	connection = runService.Heartbeat:Connect(function()
		raycastDown(bombClone, user)
	end)
	
	repeat task.wait() until activated == true
	
	connection:Disconnect()
end)

Errors:

 Explosion is not a valid member of Part "Piano"  -  Server - PlantBomb:25
  16:07:35.394  Stack Begin  -  Studio
  16:07:35.395  Script 'Workspace.Bombs.Piano.PlantBomb', Line 25 - function explosionParticles  -  Studio - PlantBomb:25
  16:07:35.395  Script 'Workspace.Bombs.Piano.PlantBomb', Line 47 - function explode  -  Studio - PlantBomb:47
  16:07:35.395  Script 'Workspace.Bombs.Piano.PlantBomb', Line 173 - function raycastDown  -  Studio - PlantBomb:173
  16:07:35.395  Script 'Workspace.Bombs.Piano.PlantBomb', Line 191  -  Studio - PlantBomb:191
  16:07:35.395  Stack End  -  Studio
1 Like

On what exact line occurs the problem? Can you show it


image

I think the explode (or the explosionParticles) function is more the cause of the problem can you show the code of it?

local baseDamage = 50

function explosionParticles(bomb)
	local explosionClone = bomb.Explosion:Clone()
	explosionClone.Parent = bomb
	explosionClone.Enabled = true

	return explosionClone
end

function explode(bomb, user, pianoDropCFrame)
	for i = 1, 3 do task.wait(1)
		bomb:WaitForChild("tickHighlight").FillTransparency = 0
		bomb:WaitForChild("tickHighlight").OutlineTransparency = 0
		
		tickSound:Play()
		
		bomb:WaitForChild("tickHighlight").FillTransparency = 1
		bomb:WaitForChild("tickHighlight").OutlineTransparency = 1
	end

	task.wait(1)

	bomb.Anchored = true -- Anchores the bomb so that it doesn't go elsewhere due to the explosion.

	local explosionParticle = explosionParticles(bomb)
	local explosionSoundClone = explosionSound:Clone()
	explosionSoundClone.Parent = bomb
	explosionSoundClone:Play()

	explosionSound:Play()

	bomb.Transparency = 1

	local region3 = Region3.new(
		bomb.Position - Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W),
		bomb.Position + Vector3.new(blastRegion3L, blastRegion3H, blastRegion3W) -- Makes a square that is 5x5 long and wide, and 10 studs high (hence the 1 to 10)
	) -- Creates a region3 that destroys any parts in the explosions radius.

	local region3Visual = Instance.new("Part") -- The visualized part for the region3.
	region3Visual.Name = "blastRadiousVisual"
	region3Visual.CanCollide = false
	region3Visual.Size = region3.Size
	region3Visual.CFrame = region3.CFrame
	region3Visual.Anchored = true
	region3Visual.Transparency = .5
	region3Visual.Parent = workspace.Terrain
	region3Visual.BrickColor = BrickColor.new("Persimmon")

	local explosion = Instance.new("Explosion") -- The explosion instance
	explosion.Parent = bomb
	explosion.Position = bomb.Position
	explosion.BlastRadius = 0
	
	explosion.DestroyJointRadiusPercent = 0
	explosion.BlastPressure = 900000

	explosion.BlastRadius = region3.Size.Z

	local overlapParams = OverlapParams.new()
	overlapParams.FilterDescendantsInstances = {tool, tool.Handle, region3Visual}

	local parts = workspace:GetPartBoundsInBox(region3.CFrame, region3.Size, overlapParams)

	explosion.Hit:Connect(function(Part)
		if Part.Name == "HumanoidRootPart" then
			local character = Part.Parent
			local player = game.Players:GetPlayerFromCharacter(character)

			if player and player.Team ~= user.Team then
				character:FindFirstChildOfClass("Humanoid"):TakeDamage(baseDamage)

				if character:FindFirstChildOfClass("Humanoid").Health == 0 then
					user.leaderstats.Kills.Value += 1
				end

			elseif player ~= user and player.Neutral == true then
				character:FindFirstChildOfClass("Humanoid"):TakeDamage(baseDamage)

				if character:FindFirstChildOfClass("Humanoid").Health == 0 then
					user.leaderstats.Kills.Value += 1
				end
			end
		end
	end)

	for i, v in pairs(parts) do
		if v.Name == "Brick" then
			v.Anchored = false

			v.CanTouch = false

			v.BrickColor = user.TeamColor
			
			for i, constraint in pairs(v:GetChildren()) do
				if constraint:IsA("Snap") or constraint:IsA("Weld") or constraint:IsA("WeldConstraint") then
					constraint:Destroy()
				end
			end

			task.delay(2, game.Destroy, v)
		end

		if v.Name == "GameBrick" then v.Name = "TaggedGameBrick" -- TaggedGameBricks are GameBricks in the game that have been effected by the bomb, while regular Bricks are just bricks you can destroy by default.
			v.Anchored = false

			v.CanTouch = false

			user.leaderstats.Bricks.Value += 1 -- The total bricks the player has broken throughout the round.
			user["T. Bricks"].Value += 1 -- The total bricks that the player has destroyed throughout there playtime
			user.leaderstats.Studs.Value += .1 -- Increases the players currency (Studs) by 0.1.
			v.BrickColor = user.TeamColor -- Changes the taggedBricks color to the local players team color to show that they have broken the part.

			task.delay(2, game.Destroy, v) -- later deletes the part.
		end
	end

	task.delay(3, game.Destroy, bomb)
	task.delay(3, game.Destroy, region3Visual)

	task.wait(2)

	explosionParticle.Enabled = false -- Disables the explosion particles.
end

image
image

Seems alright i guess, whatever Piano is in your raycast filter is not syntaxed right probally

You don’t really need to disconnect it

connection = runService.Heartbeat:Connect(function()
	if activated then
			raycastDown(bombClone, user)
	end
end)

Seems like the line that changes the activated variable to true isn’t being reached because it errors within the explode() function.

I believe this line here is the culprit within the explode() function

The errors you received shows that there is no child of the part “piano” (in this case the variable bomb) called Explosion. Either the Explosion object was never added to the piano beforehand, or it is still loading when it trys to index it.

Make sure that the part Piano has the particle emitter within it. If it does, then in the code you should use :FindFirstChild("Explosion") (So it doesn’t error) and create a case for when it can’t find that particle emitter.

1 Like