First of all, you don't seem to have any debounce. Touched event fires multiple times for humanoids and on top of that you probably don't want the function to fire again for other characters. If you do, your script needs to be completely rewritten. But first things first, add a variable that will remeber if the event was fired already:
local fired = false
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not fired then
fired = true
head = hit.Parent.Head
torso = hit.Parent.Torso
script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0)
weld = Instance.new("Weld")
weld.Part0 = torso
weld.C0 = torso.CFrame:inverse()
weld.Part1 = script.Parent
weld.C1 = script.Parent.CFrame:Inverse()
weld.Parent = script.Parent
script.Parent.Anchored = false
end
end)
It you don't want the script to run ever again, it is good idea to disconnect the event for the performance sake:
local fired = false
local connection = script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not fired then
fired = true
connection:Disconnect()
head = hit.Parent.Head
torso = hit.Parent.Torso
script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0)
weld = Instance.new("Weld")
weld.Part0 = torso
weld.C0 = torso.CFrame:inverse()
weld.Part1 = script.Parent
weld.C1 = script.Parent.CFrame:Inverse()
weld.Parent = script.Parent
script.Parent.Anchored = false
end
end)
Also you really should unachor the part first, otherwise your weld will not work. Also best welds to use with humanoids are motor6d welds for some reason (don't ask me why, they just work better)
local fired = false
local connection = script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and not fired then
fired = true
connection:Disconnect()
script.Parent.Anchored = false
head = hit.Parent.Head
torso = hit.Parent.Torso
script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0)
weld = Instance.new("Motor6D")
weld.Part0 = torso
weld.C0 = torso.CFrame:Inverse() * script.Parent.CFrame
weld.Part1 = script.Parent
weld.Parent = torso
end
end)
Finally you don't need to use weld.C1 property, unless you plan to move/rotate welded part in very specific way... I suggest changing C0 instead. Also parent your welds to the same part as Part0.
Edit: I am not sure what line script.Parent.CFrame = head.CFrame * CFrame.new(0,5,0) should do, but if you want part welded 5 studs above head use script.Parent.CFrame = head.CFrame + Vector3.new(0,5,0)
1 comment
is the part anchored?