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
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?
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.)
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)
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)