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

How to make a moving part moves over a plate and then the part gets destroyed?

local part = script.Parent
local touched = false

local function TouchFunction(part)
	if part:IsA("Part") then 
		touched = true
	end
end

	while wait(0.5) and touched == false do
	part.CFrame = part.CFrame + Vector3.new(0,0,-1)
	
	if touched == true then
		part:Destroy()
	end
end

game.Workspace.Plate.Touched:Connect(TouchFunction)

Thats my code. The part moves, but if the part moves over the plate it doesn't get destroyed and it moves on

2 comments

0

Why are you combining Part.CFrame and Vector3.new()? Just do part.CFrame * CFrame.new(0,0,-1)

0

Because I learned it so. Im rlly new. I've been scripting for 3 days right now. And this doesn't fix my problem

1 answer

What I recommend is using the TweenService, but if you do it like this then you must. Here is your issue, what I believe is happening is that this part is anchored and you are changing its position with CFrame, so the parts aren't actually touching, because it isn't really moving you are just changing its position.

Go do some research into TweenService and the movement of parts using it, I will drop a link below.

TweenService

I do have an example (from the DevForum) of precisely what you are looking for:

local TweenService = game:GetService("TweenService")
 
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Color = Color3.new(1, 0, 0)
part.Anchored = true
part.Parent = game.Workspace
 
local goal = {}
goal.Position = Vector3.new(10, 10, 0)
goal.Color = Color3.new(0, 1, 0)
 
local tweenInfo = TweenInfo.new(5)
 
local tween = TweenService:Create(part, tweenInfo, goal)
 
tween:Play()

In the script above what is happening is you are referencing the service, and then creating a part and goal. The goal will be the ending position and color of the part, and then the TweenInfo is the length of the travel of the part. You create the local variable and then :Play() the tween.

Hope this helped, if it did please accept my answer, other than that, have a great day and happy coding!

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