Currently working on a server script that pushes you away from a part when you touch it, it worked just fine by just selecting the LookVector but I wanted it to be more self efficient and just make it so if it touches any part it’ll find which one it was and apply force away from that face, however I have no clue how to find these faces while using Touched, here’s what I got so far.
local parent = script.Parent
debounce = false
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
if not debounce then
task.wait(0.3)
debounce = true
hit.Touched:Connect(function(part)
--// Checking for what the player is currently touching and if its the parent of this script.
if part.Name == parent.Name then
--// Code goes here
--//^
end
end)
debounce = false
end
end
end)
Raycast from the parts involved in the collision, for example between the player’s part and the touched part. The result of a successful raycast gives you a Normal, which is the direction you should be pushed away.
got this so far but one small issue, the raycast doesnt seem to be able to find the part its supposed to touch.
local parent = script.Parent
debounce = false
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
if not debounce then
task.wait(0.3)
debounce = true
hit.Touched:Connect(function(part)
--// Checking for what the player is currently touching and if its the parent of this script.
if part.Name == parent.Name then
--// Code goes here
local target = hit.Parent:FindFirstChild('HumanoidRootPart')
local rayOrigin = hit.Parent.PrimaryPart.CFrame.Position
local rayDirection = part.CFrame.Position
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {hit.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
print(raycastResult)
end
--//^
end
end)
debounce = false
end
end
end)
RayDirection in your call to Raycast needs to be the direction from the start to the end of your ray, not the position of the end of the ray. So it should be something like part.CFrame.Position - rayOrigin
Here’s what I made, thank you @azqjanna and @RefusalMan for the help, if you see any issues that could be improved I shall gladly edit them .
local debugging = true
local parent = script.Parent
local cooldown = 0.4
local jumpHeight = 50
debounce = false
local rayPartConfig = require(script.RayLaserConfig)
--// Checking for what has touched the part
parent.Touched:Connect(function(hit)
if (hit.Parent:FindFirstChild('Humanoid')~=nil) then
local char = hit.Parent
local HRP = char:FindFirstChild('HumanoidRootPart')
hit.Touched:Connect(function(part)
--// Checking for what the player is currently touching and if its the parent of this script.
if part.Name == parent.Name then
if not debounce then
debounce = true
--// Code goes here
local target = hit.Parent:FindFirstChild('HumanoidRootPart')
local rayOrigin = hit.Parent.PrimaryPart.CFrame.Position
local rayDirection = part.CFrame.Position - rayOrigin
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {hit.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
if raycastResult then
if raycastResult.Normal then
local raycastNormal = raycastResult.Normal
local Att0 = Instance.new('Attachment'):Clone()
local linearVelocity = Instance.new('LinearVelocity'):Clone()
Att0.Name = 'linearVelocityAtt0'
Att0.Parent = HRP
linearVelocity.VelocityConstraintMode = 'Line'
linearVelocity.RelativeTo = 'World'
linearVelocity.LineDirection = raycastNormal
linearVelocity.LineVelocity = jumpHeight*2
linearVelocity.MaxForce = 100000
linearVelocity.Attachment0 = Att0
linearVelocity.Name = 'jumpadLForce'
linearVelocity.Parent = HRP
if debugging then
print('[Jumpad]',hit.Parent.Name,'bounced off',parent.Name,'at',linearVelocity.LineVelocity,'LineVelocity','from',raycastResult.Distance,'studs')
end
task.wait(cooldown)
linearVelocity:Destroy()
Att0:Destroy()
--//^
end
end
task.wait(cooldown)
debounce = false
end
end
end)
end
end)