Model going through walls

My issue is that when the model is moving, it will clip through anything. I would like to know how I would prevent this happening in the game. I use CFrame to move the model. I’ve not tried any solutions yet because I have no idea what to do. CanCollide is enabled for all the parts in the model.


Here is the script inside of StarterPlayerScripts

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local CollectionService = game:GetService("CollectionService")

-- Stats/Properties
local radius = 50
local speed = 15

-- Connect to the Heartbeat event to run the following code every frame
RunService.Heartbeat:Connect(function(deltaTime)
	for _, skibidiToilet in ipairs(CollectionService:GetTagged("Toilet")) do
		-- Find the closest player within the radius
		local closestPlayer = nil
		local closestDistance = math.huge

		for _, player in ipairs(Players:GetPlayers()) do
			if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
				local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
				local distance = (skibidiToilet.PrimaryPart.Position - HumanoidRootPart.Position).Magnitude

				-- Check if the player is within the radius and closer than the current closest player
				if distance <= radius and distance < closestDistance then
					closestPlayer = player
					closestDistance = distance
				end
			end
		end

		-- If there is a closest player within the radius
		if closestPlayer then
			print("Nearest player:", closestPlayer.Name)

			local HumanoidRootPart = closestPlayer.Character:FindFirstChild("HumanoidRootPart")
			if HumanoidRootPart then
				local DirectionToPlayer = (HumanoidRootPart.Position - skibidiToilet.PrimaryPart.Position).unit

				-- Calculate the new position of the model
				local newPosition = skibidiToilet.PrimaryPart.Position + DirectionToPlayer * speed * deltaTime
				newPosition = Vector3.new(newPosition.X, skibidiToilet.PrimaryPart.Position.Y, newPosition.Z) -- Keep the same Y-level 

				-- Calculate the rotation to face the opposite direction
				local oppositeDirection = -DirectionToPlayer
				local rotationAngle = math.atan2(oppositeDirection.X, oppositeDirection.Z)
				local rotation = CFrame.Angles(0, rotationAngle, 0)

				-- Update the model's position and rotation
				skibidiToilet:SetPrimaryPartCFrame(CFrame.new(newPosition) * rotation)
			end
		else
			print("No player in radius")
		end
	end
end)

-- Connect the Touched event for each Hitbox
for _, skibidiToilet in ipairs(CollectionService:GetTagged("Toilet")) do
	local hitbox = skibidiToilet:WaitForChild("Hitbox")
	if hitbox then
		hitbox.Touched:Connect(function(hit)
			local character = hit.Parent
			if character:IsA("Model") and character:FindFirstChild("Humanoid") then
				character.Humanoid.Health = 0
				print(character.Name .. " was touched and killed.")
			else
				print("Hitbox was touched, but it did not kill the player.")
			end
		end)
		print("Skibidi Toilet instance found and Touched event connected.")
	else
		print("Skibidi Toilet Hitbox not found.")
	end
end

try looking into pathfinding

What the script is doing is basically forcing the parts to certain cframes, which does not get affected by collisions at all because (I assume) they’re anchored. As KingBob said you’ll likely have to look into pathfinding and to not move the models through just setting their positions. Maybe look into AlignPositions?

Not only pathfinding but is the item cancollide turned off? The mesh should have boundaries if the cancollide is turned on.

They mentioned cancollide was on for all the parts

I missed this, just noticed it when I reread it, oops!

1 Like

Definitely seems like a hitbox issue, there does seem to be collision with the models in the video but they just ‘pop’ right through. Maybe try to give them a bigger hitbox?