Raycasting not working w/ Tweening a fast cylinder

I have a cylinder and I want to tween it to a part, but it will stop if it hits any walls, except for some reason it breaks and it’s only doing this cause it’s tweening a cylinder way too quickly, but I want it to quickly tween it and I want my part to be a cylinder. It works fine if I tween a cube really quickly, but if I use a cylinder and tween it quickly it all of a sudden breaks. Also, if the raycast hits a Humanoid it damages the Humanoid.

Any fixes?

My code:

local TS = game:GetService("TweenService")
local Part = script.Parent.Cylinder
local FaceV = script.Parent.FaceVector
local Time = 0.3
local TSInfo = TweenInfo.new(

	Time,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

wait(5)

local Animation = TS:Create(Part, TSInfo, {CFrame = script.Parent.Z.CFrame; Size = script.Parent.Z.Size})

Animation:Play()

while wait(0) do
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {script.Parent}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast = workspace:Raycast(Part.Position,FaceV.CFrame.LookVector * 5000, Params)
	
	if Raycast then
		
		local Distance = (Part.Position - Raycast.Position).Magnitude
		
		if Distance <= Part.Size.Z /2  then
			Animation:Pause()
			Animation:Destroy()
			if Raycast.Instance.Parent:FindFirstChild("Humanoid") then
				print("hit")
				Raycast.Instance.Parent.Humanoid:TakeDamage(5)
			end
			break
		end
	end
end
1 Like

instead of doing while wait() i suggest you use runservice.Stepped() it is much more synced with the game

now with the raycasting, have you made a debug part to see if the ray is actually pointing in the right direction?

Isn’t RunService.Stepped() only on Local Scripts? Because this IS a Server Script.

Also, how exactly do I debug the RayCast?

no stepped can be used in any script, server or client. only renderstepped can be used on the client

debug a raycast with a part
How do you visualize a raycast as a part? - Help and Feedback / Scripting Support - DevForum | Roblox

Bit of a problem, the raycast never actually hits anything, so if you’re checking it, it will not go past the if statement.

that is why i am asking for you to debug the cast, to make sure it is raycasting in the direciton you really want

I’ve found out that indeed the RunService stuff does work better than the while wait (which didn’t stop at all) and the Raycast is probably pointing in the right direction, but it is a bit delayed, the slightly transparent part is where it’s suppose to fully go, and it’s suppose to stop at the wall but it’s delayed, any fixes?

image

I see the problem now, I thought if I made a part and put it forward facing the Raycast would go in that direction, but it still goes in the direction of the cylinder… Any fixes? And yes, the face vector part is facing forward (I added a decal to make sure it was.)

image

Here is my new code:

local TS = game:GetService("TweenService")
local Part = script.Parent.Part
local FaceV = script.Parent.FaceVector
local RunService = game:GetService("RunService")
local Time = 0.3
local TSInfo = TweenInfo.new(

	Time,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

wait(5)

local Animation = TS:Create(Part, TSInfo, {CFrame = script.Parent.Z.CFrame; Size = script.Parent.Z.Size})

Animation:Play()

RunService.Stepped:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {script.Parent}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast = workspace:Raycast(Part.Position,FaceV.CFrame.LookVector * 5000, Params)
	
	if Raycast then
		
		local Distance = (Part.Position - Raycast.Position).Magnitude
		local p = Instance.new("Part")
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, Distance)
		p.CFrame = CFrame.lookAt(Part.Position, FaceV.CFrame.LookVector)*CFrame.new(0, 0, -Distance/2)
		p.Parent = workspace
		if Distance <= Part.Size.Z /2  then
			Animation:Pause()
			Animation:Destroy()
			if Raycast.Instance.Parent:FindFirstChild("Humanoid") then
				print("hit")
				Raycast.Instance.Parent.Humanoid:TakeDamage(5)
			end
		end
	end
end)

looks like the raycast is hitting the debug part
when making the part do part.CanQuery = false

then test and send another screenshot please

I accidentally made a mistake in my code to show the Raycast, whoops. :sweat_smile:


The raycast ends at the wall, but for some reason pausing the Tween is strangely delayed.
(Made the material of the Original Part Forcefield so you could see)

Here is my code (without the mistake)

local TS = game:GetService("TweenService")
local Part = script.Parent.Part
local FaceV = script.Parent.FaceVector
local RunService = game:GetService("RunService")
local Time = 0.3
local TSInfo = TweenInfo.new(

	Time,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

wait(5)

local Animation = TS:Create(Part, TSInfo, {CFrame = script.Parent.Z.CFrame; Size = script.Parent.Z.Size})

Animation:Play()

RunService.Stepped:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {script.Parent}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast = workspace:Raycast(Part.Position,FaceV.CFrame.LookVector * 5000, Params)
	
	if Raycast then
		
		local Distance = (Part.Position - Raycast.Position).Magnitude
		local p = Instance.new("Part")
		p.CanQuery = false
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, Distance)
		p.CFrame = CFrame.lookAt(Part.Position, Raycast.Position)*CFrame.new(0, 0, -Distance/2)
		p.Parent = workspace
		if Distance <= Part.Size.Z /2  then
			Animation:Pause()
			Animation:Destroy()
			if Raycast.Instance.Parent:FindFirstChild("Humanoid") then
				print("hit")
				Raycast.Instance.Parent.Humanoid:TakeDamage(5)
			end
		end
	end
end)

if it is delayed then set the position of the cylinder once the tween gets paused

it will snap it back to the right position

use the raycast.position and modify it with the cylinder’s size

What do you mean? I don’t understand. Could you give a code example?

after you pause the tween do this

Part.CFrame = CFrame.new(Raycast.Position) * CFrame.new(0, 0, -Part.Size.Z / 2)

(Sorry for late reply I was busy)
So, I think something’s broken.
image
Even with no orientation when it’s facing it head on it still breaks, though.
image

Here is my code:

local TS = game:GetService("TweenService")
local Part = script.Parent.Beam
local FaceV = script.Parent.FaceVector
local RunService = game:GetService("RunService")
local Time = 0.3
local TSInfo = TweenInfo.new(

	Time,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.InOut,
	0,
	false
)

wait(5)

local Animation = TS:Create(Part, TSInfo, {CFrame = script.Parent.Z.CFrame; Size = script.Parent.Z.Size})

Animation:Play()

RunService.Stepped:Connect(function()
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {script.Parent}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	local Raycast = workspace:Raycast(Part.Position,FaceV.CFrame.LookVector * 5000, Params)

	if Raycast then

		local Distance = (Part.Position - Raycast.Position).Magnitude
		local p = Instance.new("Part")
		p.CanQuery = false
		p.Anchored = true
		p.CanCollide = false
		p.Size = Vector3.new(0.1, 0.1, Distance)
		p.CFrame = CFrame.lookAt(Part.Position, Raycast.Position)*CFrame.new(0, 0, -Distance/2)
		p.Name = "RayCastP"
		p.Parent = script.Parent
		if Distance <= Part.Size.Z /2  then
			Animation:Pause()
			Part.CFrame = CFrame.new(Raycast.Position) * CFrame.new(0, 0, -Part.Size.Z / 2)
			Animation:Destroy()
			if Raycast.Instance.Parent:FindFirstChild("Humanoid") then
				Raycast.Instance.Parent.Humanoid:TakeDamage(5)
			end
		end
	end
end)