Alright so I’m currently trying to make a zombie grab attack where the zombie attacks the player and if the player is in a certain radius it can hold onto the character. My issue is that I’m trying to destroy the weldconstraint holding the two together but it doesn’t destroy either at all or until the zombie/player is dead. I’m really new to welds so I’m not 100% sure on how they work but I know they’re not supposed to do what they are doing.
Code:
Long Code
--//Attacking Script
local NPC = script.Parent
local players = game:GetService("Players")
local PS = game:GetService("PhysicsService")
local RS = game:GetService("RunService")
local animDebounce = false
local anims = {
["Attack"] = 15739900977;
["Grab"] = 15739906214;
["Roar"] = 15739910635;
}
function FindPlayer(Position)
local List = game.Workspace.Characters:GetChildren() or game.Workspace:GetDescendants()
local Torso = nil
local Distance = 5000
local HumanoidRootPart = nil
local Humanoid = nil
local Player = nil
for i = 1, #List do
Player = List[i]
if (Player.ClassName == "Model") and (Player ~= script.Parent) then
HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
Humanoid = Player:FindFirstChild("Humanoid")
if (HumanoidRootPart ~= nil) and (Humanoid ~= nil) and (Humanoid.Health > 0) then
if (HumanoidRootPart.Position - Position).Magnitude < Distance then
Torso = HumanoidRootPart
Distance = (HumanoidRootPart.Position - Position).Magnitude
end
end
end
end
return Torso
end
local Goal = FindPlayer(NPC.HumanoidRootPart.Position)
RS.Heartbeat:Connect(function()
if (NPC.HumanoidRootPart.Position - Goal.Position).Magnitude <= 2 then
weldChars(NPC, Goal.Parent)
if script.Parent.IsGrabbing == true then
script.Parent.Follow.Disabled = true
else
script.Parent.Follow.Disabled = false
end
end
end)
function restorePlayer(char)
char.HumanoidRootPart.Attachment1.Part0 = nil
char.HumanoidRootPart.Attachment1.Part1 = nil
char.HumanoidRootPart.Attachment1:Destroy()
local player = players:GetPlayerFromCharacter(char)
for _,v in pairs(char:GetChildren()) do
if v:IsA("BasePart") then
PS:SetPartCollisionGroup(v, "Grab")
end
end
char:FindFirstChild("Grabbed").Value = false
for _, v in pairs(char:GetChildren()) do
if v:IsA("BasePart") then
v.CollisionGroupId = 0
v.Massless = false
if player then
v:SetNetworkOwner(player)
else
v:SetNetworkOwner(nil)
end
end
end
end
function weldChars(origin, grabbed)
grabbed:FindFirstChild("Grabbed").Value = true
script.Parent.IsGrabbing.Value = true
animPlay("Grab")
local ro, rc = origin.HumanoidRootPart, grabbed.HumanoidRootPart
grabbed.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
grabbed.Humanoid.PlatformStand = true
local att1 = Instance.new("WeldConstraint")
att1.Parent = rc
att1.Part0 = rc
att1.Part1 = ro
att1.Name = "Attachment1"
local objectValue = Instance.new("ObjectValue")
objectValue.Name = "GrabbedVictim"
objectValue.Value = grabbed
objectValue.Parent = origin
for _,v in pairs(origin:GetChildren()) do
if v:IsA("BasePart") then
PS:SetPartCollisionGroup(v, "Grab")
end
end
for _,v in pairs(grabbed:GetChildren()) do
if v:IsA("BasePart") then
v.Massless = true
PS:SetPartCollisionGroup(v, "Grab")
end
end
repeat wait(.1) until grabbed.Humanoid.Health == 0 or origin.Humanoid.Health == 0
grabbed:FindFirstChild("Grabbed").Value = false
script.Parent.IsGrabbing.Value = false
att1:Destroy()
if (grabbed:FindFirstChild("Grabbed").Value == false or script.Parent.IsGrabbing.Value == false) and origin:FindFirstChild("GrabbedVictim") == objectValue then
restorePlayer(grabbed)
objectValue:Destroy()
end
end
function animPlay(anim, cd)
if animDebounce == false then
animDebounce = true
if script.Parent.IsGrabbing.Value == true then
return
else
if anim == "Grab" then
anims[anim]:Play()
repeat wait() until Goal.Parent:WaitForChild("Grabbed").Value == false
else
anims[anim]:Play()
task.wait(cd)
end
end
animDebounce = false
end
end
Video on what is wrong:
thanks,
dza