So I am making a forcefield which sends sparks when the player touches it. It also pushes the player back as a force. I am using BodyVelocity for this but I am very new to this concept. I searched up a few solutions and I am trying them but when I test it, nothing is happening?
Script:
local Force = -50
local db = false
workspace.Part.Touched:Connect(function(hit)
if db == false then
if hit.Parent:FindFirstChild("Humanoid") then
db = true
local char = hit.Parent
local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
local BodyForce = Instance.new("BodyVelocity")
local direction = char.HumanoidRootPart.CFrame.LookVector
BodyForce.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
BodyForce.Velocity = direction * Force
print("SENT PLAYER FLYING")
game.Debris:addItem(BodyForce, 5)
wait(5)
db = false
end
end
end)
Looks like you’re not setting the BodyVelocity’s parent, so it’s not applying. Parent it to the HumanoidRootPart.
It also looks like you’re using the CFrame.LookVector of the HumanoidRootPart, which might be problematic as you cannot guarantee its direction when the force-back part is touched. Use the CFrame of the part itself, which won’t change.
Also, you’ll want to make a debounce that is per-touching-humanoid, not overall with the db variable. Perhaps do a check for the existence of the BodyVelocity within the HumanoidRootPart.
local part = workspace.Part
part.Touched:Connect(function (hit)
-- ...
BodyForce.Velocity = part.CFrame.LookVector * Force
-- ^ note: this is part (the wall), not hit (the touching limb)
-- ...
end)
LookVector points in the same direction as the CFrame, so this will force them in the direction of the wall part. Consider using UpVector or RightVector and changing the polarity of your Force variable accordingly to ensure you’re getting the correct direction. In Studio, you can right-click the part and hit “Show Orientation Indicator” to see which way LookVector faces.