Greetings developers! I am messing around with CFrames to try and learn how to use them and I want my part, CubeDude to follow the player around. To do this I need to get the Torso’s position, but when I do local char = game.Players.LocalPlayer.Character. Torso isnt one of the children. How come? Here is my full code:
local char = game.Players.LocalPlayer.Character.
local CubeDude = script.Parent
local GreenyBeany = game.Workspace.GreenyBeany
CubeDude.CFrame = CFrame.new(char.po) --This was me trying to get the Char's position. But now I know I am looking for the Torse position.
Not much but I am just messing around at the moment.
It is because character isn’t loaded. Use it instead:
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local CubeDude = script.Parent
local GreenyBeany = game.Workspace.GreenyBeany
CubeDude.CFrame = CFrame.new(char.po) --This was me trying to get the Char's position. But now I know I am looking for the Torse position.
You don’t really need to get the character’s torso position. You need their rootpart.
Down below, I have provided you with a LocalScript, (thats meant to be put in StarterCharacterScripts).
This will make your “Cubedude” follow you.
Put your “Cubedude” inside of replicated storage for this to work.
local runservice = game:GetService("RunService")
local cubedude = game:GetService("ReplicatedStorage").CubeDude
local cubeclone = cubedude:Clone()
local speed = 0.3
local char = script.Parent
local humanoidrootpart = char:WaitForChild("HumanoidRootPart")
cubeclone.Parent = workspace
cubeclone.CFrame = humanoidrootpart.CFrame
runservice.Heartbeat:Connect(function()
cubeclone.CFrame = cubeclone.CFrame:Lerp(humanoidrootpart.CFrame * CFrame.new(-5,3,5), speed)
end)
Here are some arcticles that you can learn from if needed!
The greater the speed, the faster the pet moves. The lesser the speed, the slower the pet moves.