this is the code i use when a certain player uses a tool
local CharPos = Char.HumanoidRootPart.Position local Region = Region3.new(CharPos-Vector3.new(10,10,10),CharPos+Vector3.new(10,10,10)) local RTable = workspace:FindPartsInRegion3(Region, nil, 20) for i,v in pairs (RTable) do if v.Parent ~= Char and v.Parent:FindFirstChild("HumanoidRootPart") ~= nil and v.Parent:FindFirstChild("Humanoid") ~= nil then print(v.Parent.Name) local humanoidRootPart = v.Parent:findFirstChild("HumanoidRootPart") local bodyv = Instance.new("BodyVelocity",humanoidRootPart) bodyv.MaxForce = Vector3.new(40000,40000,40000) bodyv.Velocity = humanoidRootPart.CFrame.LookVector * -100 game.Debris:AddItem(bodyv,0.2) end end
it works but when the player faces away from the "certain player" then he gets pulled in instead, Is there away way to fix that?
Yes! There is a way to fix that. It has to do with the bodyv.Velocity
. When you set it we need to make sure that we are setting it dynamically based off where the repelling player is and not just where their own HumanoidRootPart is facing.
With your current code the player being repelled will always be pushed 100 studs backwards away from where they're facing humanoidRootPart.CFrame.LookVector * -100
and not away from the player repelling them.
Try this:
bodyv.Velocity = (CharPos - humanoidRootPart.Position).Unit*-100