Pause tween when touched part

I’ve created a simple test script for a star-wars inspired laser gun I’ll be making soon, and what I’ve got at the moment works fine, but what I’d like to happen is the laser part is destroyed upon touching any part. I can easily do the logic to make a character take damage and stuff but I can’t figure out how to make the Touched event fire in the first place. Here is the code I have right now, feel free to comment on any improvements I can make if you notice any anyway:

local TweenService = game:GetService("TweenService")

local ray = workspace.Ray

local target = workspace.Target

local origin = workspace.Origin

local goal = {}

goal.Position = target.Position

local distance = (origin.Position - target.Position).magnitude

local tweenInfo = TweenInfo.new(

0.5*distance/100,

Enum.EasingStyle.Linear,

Enum.EasingDirection.Out,

0,

false,

0

)

ray.CFrame = CFrame.new(origin.CFrame.Position, target.CFrame.Position) * CFrame.Angles(0, 
math.rad(90), 0)

local tween = TweenService:Create(ray, tweenInfo, goal)

wait(2)

tween:Play()

Thanks.

1 Like

So, I am pretty new to this stuff too but I think you could make a hit variable and then use FindPartOnRay to get the object hit. Then just check if whatever the ray hit is a player, and then do damage.

More on rays here - https://developer.roblox.com/articles/Raycasting

EDIT: If the ray hits someone/something you can pause the tween aswell

There’s no Touched event for a ray. You simply need to destroy the part at the same time that it hits something via FindPartOnRay.

1 Like

oh, i realised i named my ray variable quite poorly. It’s not actually a ray - I just called it ray because it’s like the lasers coming from the blaster. It’s a part that is tweened, sorry, I didn’t make that very clear. I could use a ray as well, but that would slow the code down.

I sorted it out, I’m using :GetTouchingParts() on the ray object, I’ll rename it to laser as well to prevent confusion.