I am trying to make a part that when a player how has the tool can hit the part and it goes in the direction of the player’s looking spot. I am trying to make the part deflect off and objects that it hits. In the script it does that but the seconded ‘if…then’ it make that part deflect not the part. Can someone help me do this, or find a better way to do it
local part = script.Parent -- Assuming the script is a child of the part you want to move
local TweenService = game:GetService("TweenService")
local speed = 50
local function onTouch(hit)
local character = hit.Parent
local tool = character:FindFirstChildOfClass("Tool")
if tool and tool:FindFirstChild("Handle") then
local direction = character:GetPrimaryPartCFrame().lookVector
local distance = 50 -- Adjust the distance as needed
local duration = 1 -- Adjust the duration of the tween as needed
local endPosition = part.Position + direction * distance
local tweenInfo = TweenInfo.new(duration)
local goal = {}
goal.Position = endPosition
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
end
if hit:IsA("Part") then
-- Reflect the part off the wall
local normal = (part.Position - hit.Position).unit
local reflection = 2 * (part.Velocity:Dot(normal)) * normal - part.Velocity
part.Velocity = reflection
end
end
part.Touched:Connect(onTouch)
-- Set the part in motion
part.Velocity = Vector3.new(0, 0, speed)