Destroy script issue

Hello, So what I am trying to do is if the clone part touches something the thing it touched gets destroyed I have tried many ways to do this but I cant figure out how.

Here is my script.

local testplayer = game.Workspace.TestPlayer
local userInputService = game:GetService(“UserInputService”)
local tweenService = game:GetService(“TweenService”)

local cooldown = 1.5

local tweenInfo = TweenInfo.new(

1, --Time

Enum.EasingStyle.Linear, --Easing Style

Enum.EasingDirection.Out, --EasingDirection

-1, --Repeat Count

false, --Reverse

0 --DelayTime

)

userInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Space then
if cooldown == 1.5 then
cooldown = 0

	    --Laser
	    local clone = Instance.new("Part")
	    clone.Parent = game.Workspace
	    clone.Name = "Laser"
	    clone.CastShadow = false
	    clone.Anchored = true
	    clone.Material = "Neon"
	    clone.BrickColor = BrickColor.new("Really red")
	    clone.Size = Vector3.new(1, 1, 4)
		clone.CFrame = testplayer.CFrame
	    local tween = tweenService:Create(clone, tweenInfo, {Position = game.Workspace.Target.Position})
		tween:Play()
	    wait(0.8)
		clone:Destroy()
		wait(0.5)
		cooldown = 1.5
	end
end

end)

1 Like

I don’t know much about user input service, but I think you have to check the input type before you check the keycode

So you want the cloned part to destroy anything it touches, right?

Well from what I can see on your script, you haven’t added anything that could check if the part has been touched. Also, you are deleting the cloned part.

Sorry here is my updated script.

local testplayer = game.Workspace.TestPlayer
local userInputService = game:GetService(“UserInputService”)
local tweenService = game:GetService(“TweenService”)

local cooldown = 1.5

local tweenInfo = TweenInfo.new(

1, --Time

Enum.EasingStyle.Linear, --Easing Style

Enum.EasingDirection.Out, --EasingDirection

-1, --Repeat Count

false, --Reverse

0 --DelayTime

)

userInputService.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.Space then
if cooldown == 1.5 then
cooldown = 0

	    --Laser
	    local clone = Instance.new("Part")
	    clone.Parent = game.Workspace
	    clone.Name = "Laser"
	    clone.CastShadow = false
	    clone.Anchored = true
	    clone.Material = "Neon"
	    clone.BrickColor = BrickColor.new("Really red")
	    clone.Size = Vector3.new(1, 1, 4)
		clone.CFrame = testplayer.CFrame
	    local tween = tweenService:Create(clone, tweenInfo, {Position = game.Workspace.Target.Position})
		tween:Play()
		clone.Touched:Connect(function(hit)
			hit.Parent:Destroy()
			clone:Destroy()
		end)
	    wait(0.8)
		clone:Destroy()
		wait(0.5)
		cooldown = 1.5
	end
end

end)

.Touched does not work because it requires objects to physically touch. Tweening on the other hand interpolates values and does not move them physically.

So instead, we can utilize a Region3 loop.

local UIS = game:GetService("UserInputService")
local TWS = game:GetService("TweenService")

local testPlayer = workspace:FindFirstChild("TestPlayer")

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.Space then
		local clone = Instance.new("Part")
		clone.Parent = workspace
		clone.Name = "Laser"
		clone.CastShadow = false
		clone.Anchored = true
		clone.Material = "Neon"
		clone.BrickColor = BrickColor.new("Really red")
		clone.Size = Vector3.new(1, 1, 4)
		clone.CFrame = testPlayer.PrimaryPart.CFrame
		
		TWS:Create(clone, TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1, false, 0), {Position = workspace.Target.Position}):Play()
		
		clone:GetPropertyChangedSignal("Position"):Connect(function()
			local params = OverlapParams.new()
			params.FilterDescendantsInstances = {testPlayer, clone} -- BLACKLIST FOR OBJECTS THAT CAN'T BE DESTROYED
			params.FilterType = Enum.RaycastFilterType.Blacklist		
			
			local arr: {BasePart} = workspace:GetPartBoundsInBox(clone.CFrame, clone.Size, params)
			
			for _,v: BasePart in ipairs(arr) do
				v:Destroy()
			end
		end)
	end
end)

The input script works I’m just trying to make it if the clone part touches something that it will destroy the part its touching.

So will this script do what I need it to do?

1 Like

You can copy the code and modify it to your needs.

I included a video which showcases what the script does.