I made a tool that grabs other players/NPCs, and ragdolls them. For some reason when my character enters falling state, it starts to fly. Is this supposed to happen? How do I fix it?
I didn’t try any solutions at all because this issue was so weird that I couldn’t recognize where was the problem.
By the way, if there was any solution to other error that happens (grabbed part separates from the body), I would also appreciate it.
code (regular script)
local tool = script.Parent
local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local Slash = script:WaitForChild("Slash")
local Debounce = false
local PlayersHit = {}
-- Animate
Tool.Activated:Connect(function()
if Debounce == false then
Debounce = true
local Humanoid = Tool.Parent:WaitForChild("Humanoid")
local Humanoid = Tool.Parent:WaitForChild("Humanoid")
local AnimTrack = Humanoid:LoadAnimation(Slash)
AnimTrack:Play()
wait(1)
Debounce = false
end
end)
local function toggleNetworkOwnership(characterModel, isEnabled, owner)
for _, child in ipairs(characterModel:GetDescendants()) do
if child:IsA("BasePart") then
if child.Anchored then
child.Anchored = false
end
if isEnabled then
child.Massless = false
child:SetNetworkOwner(owner)
else
child.Massless = true
child:SetNetworkOwner(nil)
end
end
end
end
local function enableRagdoll(characterModel, player, enabled)
if enabled then
if player then
-- set the network ownership to the server to allow better ragdolling
toggleNetworkOwnership(characterModel, false, player)
end
-- create a bunch of temporary attachments and constraints so they flop around
for _, v in pairs(characterModel:GetDescendants()) do --ragdoll
if v:IsA("Motor6D") then
local a0, a1 = Instance.new("Attachment"), Instance.new("Attachment")
a0.CFrame = v.C0
a1.CFrame = v.C1
a0.Parent = v.Part0
a1.Parent = v.Part1
local b = Instance.new("BallSocketConstraint")
b.Attachment0 = a0
b.Attachment1 = a1
b.Parent = v.Part0
-- disable the existing character motors
v.Enabled = false
end
end
else
-- remove the ragdoll constraints and connections
for _,v in pairs(characterModel:GetDescendants()) do --unragdoll
if v:IsA('Motor6D') then
v.Enabled = true
end
if v.Name == 'BallSocketConstraint' then
v:Destroy()
end
if v.Name == 'Attachment' then
v:Destroy()
end
end
if player then
-- return network ownership to the original player
toggleNetworkOwnership(characterModel, true, player)
end
end
end
-- Damage
Tool.Part.Touched:Connect(function(Hit)
print("wenomechainsama")
if Hit.Parent:FindFirstChild("Humanoid") and Hit.Parent ~= Tool.Parent then
print("touch humanoid")
if Debounce == true and PlayersHit[Hit.Parent] == nil then
PlayersHit[Hit.Parent] = true
print("touch")
local ga0 = Instance.new("Attachment")
ga0.Parent = Tool.Part
local ga1 = Instance.new("Attachment")
ga1.Parent = Hit
local weld = Instance.new("BallSocketConstraint")
Hit.Position = Tool.Part.Position
wait()
weld.Parent = Tool.Part
weld.Attachment0 = ga0
weld.Attachment1 = ga1
weld.Enabled = true
Hit.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.PlatformStanding)
enableRagdoll(Hit.Parent, game.Players:GetPlayerFromCharacter(Hit.Parent), true)
wait(1)
PlayersHit[Hit.Parent] = nil
end
end
end)
tool.Unequipped:Connect(function()
tool.Parent.Parent.Character.Humanoid.Animator:LoadAnimation(script.Unequip)
local weld = Tool.Part:FindFirstChild("BallSocketConstraint")
if weld ~= nil then
weld.Attachment1.Parent.Parent.Humanoid:ChangeState(Enum.HumanoidStateType.FallingDown)
enableRagdoll(weld.Attachment1.Parent.Parent, game.Players:GetPlayerFromCharacter(weld.Attachment1.Parent.Parent), false)
weld:Destroy()
end
end)