I made a script when a player touches a part it flys away from them. I am wanting to know how to add a feature to my script, when the part hits another part it will bounce back or off the part.
I have an idea of how to do it but I don’t know how to do that and if that is the best way to do it.
How do I accomplish this?
local TweenService = game:GetService("TweenService")
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 = 10 -- 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
end
part.Touched:Connect(onTouch)