Push parts away from players

Hi guys! I want to add a feature to my game while walking in the library. This feature will be this:

So basically, I want to achieve the exact same like what we can see in the video.

So far I have this code:

local player = game.Players.LocalPlayer
local radius = workspace:FindFirstChild("PlayerRadius")

local function getTerrainHeight(position)
	local ray = Ray.new(position, Vector3.new(0, -1, 0) * 500)
	local ignoreList = {player.Character, radius}
	local hit, hitPosition, hitNormal = workspace:FindPartOnRayWithIgnoreList(ray, ignoreList)
	return hit and hitPosition.Y or nil
end

local function updateRadius()
	local character = player.Character
	local rootPart = character and character:FindFirstChild("HumanoidRootPart")

	if character and rootPart and radius then
		local groundHeight = getTerrainHeight(rootPart.Position)
		if groundHeight then
			radius.Position = Vector3.new(rootPart.Position.X, groundHeight, rootPart.Position.Z)
			local region = Region3.new(radius.Position - radius.Size * 0.5, radius.Position + radius.Size * 0.5)
			local collisionParts = workspace:FindPartsInRegion3(region, nil, math.huge, false)

			local pushForce = 1000
			local pushDirection = Vector3.new()
			local bodyForce = Instance.new("BodyForce")
			bodyForce.Force = pushDirection

			for _, part in ipairs(collisionParts) do
				if part:IsA("BasePart") and part ~= radius then
	
					if part.Name == "Part1" or part.Name == "Part2" then
						pushDirection = (CFrame.new(part.Position, Vector3.new(part.Position.X, 0, part.Position.Z)).LookVector).unit
						bodyForce.Force = pushDirection * pushForce
						bodyForce.Parent = part
					end
				end
			end
		end
	end
end

game:GetService("RunService").Heartbeat:Connect(updateRadius)

I want to achieve that if a player gets close to a part then it just get pushed away, where the player is facing exactly.

Thank you for your help! Have a great day!

1 Like

So what’s the problem with your code? Is it not performing as expected?