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.
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)
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
This fixed it but could you help me turn it around since it faces the player from the back
-- 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)