Problem with parts in model during motion

I want to make a model follow me and kill me, but when the model moves towards me, only the primarypart moves and not the rest of the model.
RobloxStudioBeta_alHnMOeB0f

I have considered welding the parts together but I wanna know if there is other methods. Here is the script:

-- Services
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Parts and Players
local skibidiToilet = game.Workspace:WaitForChild("Skibidi Toilet")
local localPlayer = Players.LocalPlayer

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

RunService.Heartbeat:Connect(function(deltaTime)
	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

			if distance <= radius and distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end

	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  
			local Angle = math.atan2(DirectionToPlayer.X, DirectionToPlayer.Z)  

			skibidiToilet.PrimaryPart.CFrame = CFrame.new(skibidiToilet.PrimaryPart.Position) * CFrame.Angles(0, Angle, 0) 

			local NewPosition = skibidiToilet.PrimaryPart.Position + DirectionToPlayer * speed * deltaTime  

			NewPosition = Vector3.new(NewPosition.X, skibidiToilet.PrimaryPart.Position.Y, NewPosition.Z) 

			-- Update the position of the PrimaryPart
			skibidiToilet.PrimaryPart.Position = NewPosition
		end
	else
		print("No player in radius")
	end

end)
1 Like

Welding the parts together would probably be the easiest and most performant, why don’t you want to?

local model = script.Parent
local rootpart = script.Parent.Rootpart

for i, Parts in pairs(model:GetChildren()) do
	if Parts:IsA("BasePart") then
		
		local Weld = Instance.new("WeldConstraint")
		Weld.Part0 = Parts
		Weld.Part1 = rootpart
		Weld.Parent = Parts
	end
end

So I made a welding script but the head of the toilet, but it still stays behind like in the screenshot

Here is the overview of the toilet model
RobloxStudioBeta_E08OZlJ92K

You have motor6ds for animation?

No they were just included in the model, I wasn’t intending to use them but I thought they didn’t matter if they were there.

Well then you can get rid of them and weld everything to the root part with weld constraints

Try Model:MoveTo() Instead

1 Like

Setting Primarypart.Position only moves the primary part. Use model:SetPrimaryPartCFrame(cframe) to move everything. You can also use :PivotTo()

This fixed it but could you help me turn it around since it faces the player from the back
RobloxStudioBeta_qsA1mXQi6E

-- Services
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

-- Parts and Players
local skibidiToilet = game.Workspace:WaitForChild("Skibidi Toilet")
local localPlayer = Players.LocalPlayer

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

RunService.Heartbeat:Connect(function(deltaTime)
	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

			if distance <= radius and distance < closestDistance then
				closestPlayer = player
				closestDistance = distance
			end
		end
	end

	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
			local Angle = math.atan2(DirectionToPlayer.X, DirectionToPlayer.Z)

			local adjustedSpeed = speed * deltaTime

			local newPosition = skibidiToilet.PrimaryPart.Position + DirectionToPlayer * adjustedSpeed
			newPosition = Vector3.new(newPosition.X, skibidiToilet.PrimaryPart.Position.Y, newPosition.Z)
			skibidiToilet:SetPrimaryPartCFrame(CFrame.new(newPosition) * CFrame.Angles(0, Angle, 0))
		end
	else
		print("No player in radius")
	end
end)

What you could try is just add CFrame.Angles(0,math.rad(180),0).

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.