Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a script detect a specific part and destroy it?

So I have been having a problem where I made this orb that goes around the player and it activates when you click the left mouse button, but the problem is is that I wanna make the script destroy a specific part that hits it. But it does not do that, please help. Here is the code:

local event = game.ReplicatedStorage.Events.ForcefieldEvent


event.OnServerEvent:Connect(function(player)
	local upperTorso = player.Character.UpperTorso
	local weld = Instance.new("WeldConstraint",upperTorso)
	local cFF  = Instance.new("Part",workspace)
	cFF.Anchored = false
	cFF.CanCollide = false
	cFF.Size =  Vector3.new(11.87, 11.87, 11.87)
	cFF.Shape = "Ball"
	cFF.Material = Enum.Material.SmoothPlastic
	cFF.Position = upperTorso.Position
	weld.Part0 = upperTorso
	weld.Part1 = cFF
	cFF.Transparency = 0.5
	cFF.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("BlueOrb") then
		workspace.BlueOrb:Destroy()
	else
		print("Pass.")
		end
	end)
end)

1 comment

0

Do you have a localscript which send Mouse.Target to this script?

1 answer

✓ Accepted answer

I noticed an error that pretty much renders the whole script pointless:

   cFF.Touched:Connect(function(hit)
	    if hit.Parent:FindFirstChild("BlueOrb") then
	        workspace.BlueOrb:Destroy()

"Touched" Is an event that is triggered only when a part collides into another, you should consider adding a ClickDetector inside cFF if you want the player to use the LeftMouseButton to destroy the part, and in that case the script would be

local event = game.ReplicatedStorage.Events.ForcefieldEvent


event.OnServerEvent:Connect(function(player)
	local Character =  player.Character or player.CharacterAdded:wait()   
	local upperTorso = player.Character.UpperTorso
    local weld = Instance.new("WeldConstraint",upperTorso)
    local cFF  = Instance.new("Part",workspace)
	local click = Instance.new()    
	cFF.Anchored = false
    cFF.CanCollide = false
    cFF.Size =  Vector3.new(11.87, 11.87, 11.87)
    cFF.Shape = "Ball"
    cFF.Material = Enum.Material.SmoothPlastic
    cFF.Position = upperTorso.Position
	cFF.Name = "BlueOrb"    
	weld.Part0 = upperTorso
    weld.Part1 = cFF
    cFF.Transparency = 0.5
	 local debounce = false   
	click.MouseClick:Connect(function(hit)
	if debounce == false then	    
		debounce = true	
			workspace:FindFirstChild("BlueOrb")
			        workspace.BlueOrb:Destroy()
			debounce = false
    else
        print("Pass.")
        end
    end)
end)

Also sorry if this doesn't work as I didn't have the full script and couldn't test it out, if there are any problems please comment, hope this helps!

2 comments

0

Works ty!

0

No problem, but will you please click the checkmark near my answer so that others know it's been answered?

This discussion is preserved as it was. New answers, comments, and votes are closed.