I have tried it with bodyposition but i do not know of a correct way of doing it.
How do i make a block follow the torso?
2 answers
Have you tried putting this script in the part? If you need code on supplying a part when a player joins, I'll give you that at the end.
game.Players.PlayerAdded:connect(function(plr)
plr.CharacterAdded:connect(function(plrmodel)
while wait() do
script.Parent.CFrame = CFrame.new(plrmodel.Torso.Position)
end
end)
end)
And if you need a brick for the player then you create a brick, put it in 'ReplicatedFirst', place the following ServerScript inside the part (Also place an objectvalue inside it & make the script disabled):
game.Players[script.Parent.ObjectValue].CharacterAdded:connect(function(plrmodel)
while wait() do
script.Parent.CFrame = CFrame.new(plrmodel.Torso.Position)
end
end)
and put this in the 'StarterGui' as a LocalScript
local lplr = game.Players.LocalPlayer
local replicatedstorage = game:GetService('ReplicatedStorage')
local brick = replicatedstorage.Part:Clone()
lplr.CharacterAdded:connect(plrmodel)
local brick.Parent = plrmodel
brick.Position = CFrame.new(plrmodel.Torso.Position)
brick.Script.Disabled = false
brick.ObjectValue.Value = lplr
end)
And lastly if you need a script that destroys the part when the player leaves, add this ServerScript into ServerScriptService
game.Players.PlayerLeft:connect(function(plr)
if game.Workspace:FindFirstChild(plr.Name) then
if game.Workspace[plr.Name]:FindFirstChild('Part') then
game.Workspace[plr.Name].Part:Destroy()
end
end
end)
If it doesn't work please let me know, if it does please press the ^ arrow on my answer, it motivates me to keep helping people on the site -ElmEditor :)
Here's the answer to your question, it's the solution to what I had before.
Place this script in startergui
local plr = game.Players.LocalPlayer
local char = plr.CharacterAdded:wait()
local torso = char:WaitForChild('Torso')
local head = char:WaitForChild('Head')
local dog = Instance.new('Part',char)
dog.Size = Vector3.new(1,1,1)
dog.CanCollide = false
-- Add as much part settings here
local bgyro = Instance.new('BodyGyro', dog)
bgyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
local bpos = Instance.new('BodyPosition', dog)
bpos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
while wait() do
bpos.Position = (head.CFrame * CFrame.new(2, 2, 1)).p -- makes the pet move relative to the head
bgyro.CFrame = head.CFrame -- makes the pet rotate with the player
bgyro.CFrame = bgyro.CFrame * CFrame.Angles(-math.pi/2, 0, 0) -- used to rotate the part, delete it of you don't want it to.
end
Code by XAXA, used to answer this question.
This discussion is preserved as it was. New answers, comments, and votes are closed.
