Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a part stay behind a player?

Asked by 9 years ago

I'm currently trying to figure out how I can make a part stay behind a player. Even if the player turns around, the part will turn with it along the center of the torso part. I want to accomplish this with CFrame:toWorldSpace() and CFrame:toObjectSpace(), but I have no clue how to set and change CFrame ObjectSpace Values. This is what I've got so far. This will indeed make the block stay around the player, but it doesn't stay behind the player when he turns.

So does anyone have an idea where I should put either CFrame:toObjectSpace or CFrame:toWorldSpace?

_G.Block = {}
_G.Block.Free = true
_G.Block.Character = nil

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= _G.Block.Character then
        script.Parent.Parent = hit.Parent
        _G.Block.Free = false
        _G.Block.Character = hit.Parent
            local weld = Instance.new("ManualWeld")
            weld.Part0 = script.Parent
            weld.Part1 = _G.Block.Character.Torso
            weld.C0 = CFrame.new()
            weld.C1 = _G.Block.Character.Torso.CFrame:toObjectSpace(script.Parent.CFrame)
            weld.Parent = script.Parent
    end
end)

Thanks in advance

0
Why not just weld it? Perci1 4988 — 9y
0
Alright, I managed to weld it to the player, but it gets welded to the side that touches it. I really want that if you touch it from the front or any side, it gets welded to the same preset position, in this case the back of the player. Any idea how I can do this? I editted my post with an up to date script. damagex443 325 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

Firstly, I would just use a normal Weld instead of a manual weld. Second, try this. You may have to mess about with the values of the weld's C1. You may either have to make the number positive, change the axis, adjust the number or even do all of these. All you need to do is to just experiment when it comes to welds.

_G.Block = {}
_G.Block.Free = true
_G.Block.Character = nil

script.Parent.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= _G.Block.Character then
        script.Parent.Parent = hit.Parent
        _G.Block.Free = false
        _G.Block.Character = hit.Parent
        local weld = Instance.new("Weld")
        weld.Part0 = _G.Block.Character.Torso
        weld.Part1 = script.Parent
        weld.C0 = CFrame.new()
        weld.C1 = CFrame.new(-0.5,0,0) --Change this to suit your specification.
        weld.Parent = script.Parent
    end
end)

0
Wow, thanks a lot. That was much easier than I expected it to be. damagex443 325 — 9y
0
You're welcome. I always expect things to be harder than they seem to be, that's the fun of scripting. Spongocardo 1991 — 9y
Ad

Answer this question