Alright, so here's the good news, there's nothing wrong with your code (aside from the whitespace). I'm pretty sure the problem you're having is that your bear either doesn't have a part named "Head" or, if it does it's not welded to the torso by a joint named "Neck".
As such you're going to have to do that. The rule of thumb when it comes to joints is:
weld.Part0.CFrame * C0 == weld.Part1.CFrame * C1
As such you can rearrange to easily find C0 or C1. For your case we'll keep things simple and assume C1 is CFrame.new() which is essentially the identity CFrame and we can pretend like it's not there (eg. It's like multiplying by 1).
So solving for C0 to have a weld maintain two parts relative position:
C0 = weld.Part0.CFrame:inverse() * weld.Part1.CFrame
Remember the joint needs to have the name "Neck" and must be parented under the "Torso" part.
Here's an example, although personally I recommend doing the weld beforehand as opposed to generating it as the game starts because then you can avoid all that anchoring business:
02 | local head = script.Parent.Head; |
03 | local weld = Instance.new( "Weld" , t); |
07 | weld.C 0 = t.CFrame:inverse() * head.CFrame; |
As for properly white spaced code:
01 | h = script.Parent.Humanoid |
02 | t = script.Parent.Torso |
05 | local p = Instance.new( "Part" ) |
09 | p.Position = t.Position + Vector 3. new(math.random(- 50 , 50 ), 0 ,math.random(- 10 , 10 )) |
10 | p.Parent = script.Parent |
11 | h:MoveTo(Vector 3. new(p.Position.X,p.Position.Y,p.Position.Z)) |
12 | wait(math.random( 2 , 3 )) |