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

Ragdoll script not working properly?

Asked by
Ekkoh 635 Moderation Voter
9 years ago

I decided I'd like a ragdoll script for my game so I tried making my own and letting the common free model ragdoll script guide me. I didn't copy it exactly, but I'm curious what I'm missing here. I anchored the torso just so the character didn't fling, and I notice that the limbs rarely stay loosely connected to my torso (which is what I want) and they usually fling off, despite me having glued them to the torso. If anyone knows more about glues and welds than I do, I'd appreciate any feedback. I just really don't want to use a free model and I wanna figure out how to do this on my own. You can actually disregard the waitForChild function, it works fine.

function waitForChild(parent, childName)
    local function round(n, dp)
        local dp = dp or 2
        return math.floor((n * (10 ^ dp)) + .5) / (10 ^ dp)
    end
    local child = parent:FindFirstChild(childName)
    local expired = 0
    while not child do
        if expired < 3 then
            expired = expired + wait(0)
        elseif expired >= 3 and expired < 7 then
            local t = tick()
            parent.ChildAdded:wait()
            expired = expired + (tick() - t)
        else
            print("waitForChild >> Failed to find " .. parent.Name .. "." .. childName .. " after 7 seconds :(")
            return nil
        end
        child = parent:FindFirstChild(childName)
    end
    print("waitForChild >> Found " .. parent.Name .. "." .. childName .. (expired <= 0 and " immediately." or " after " .. round(expired, 3) .. "s."))
    return child
end

local char = script.Parent
local hum = waitForChild(char, "Humanoid")
local torso = waitForChild(char, "Torso")
local mods = {}

hum.Died:connect(function()
    print(script.Name .. " >> Attempting ragdoll for " .. char.Name .. "...")
    hum.Parent = nil
    torso.Anchored = true
    local head = char:FindFirstChild("Head")
    local rarm = char:FindFirstChild("Right Arm")
    local larm = char:FindFirstChild("Left Arm")
    local rleg = char:FindFirstChild("Right Leg")
    local lleg = char:FindFirstChild("Left Leg")
    if head then
        local neck = Instance.new("Weld")
        neck.Name = "Neck"
        neck.Part0 = torso
        neck.Part1 = head
        neck.C0 = CFrame.new(0, 1.5, 0)
        neck.C1 = CFrame.new()
        neck.Parent = torso
    end
    if rarm then
        rarm.CFrame = torso.CFrame * CFrame.new(1.5, 0, 0)
        local j = Instance.new("Glue")
        j.Name = "RightShoulder"
        j.Part0 = torso
        j.Part1 = rarm
        j.C0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.C1 = CFrame.new(-0.5, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.Parent = torso
    end
    if larm then
        larm.CFrame = torso.CFrame * CFrame.new(1.5, 0, 0)
        local j = Instance.new("Glue")
        j.Name = "LeftShoulder"
        j.Part0 = torso
        j.Part1 = larm
        j.C0 = CFrame.new(-1, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.C1 = CFrame.new(0.5, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.Parent = torso        
    end
    if rleg then
        rleg.CFrame = torso.CFrame * CFrame.new(.5, -2, 0)
        local j = Instance.new("Glue")
        j.Name = "RightHip"
        j.Part0 = torso
        j.Part1 = rleg
        j.C0 = CFrame.new(1, -1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.C1 = CFrame.new(0.5, 1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.Parent = torso            
    end
    if lleg then
        lleg.CFrame = torso.CFrame * CFrame.new(-.5, -2, 0)
        local j = Instance.new("Glue")
        j.Name = "LeftHip"
        j.Part0 = torso
        j.Part1 = lleg
        j.C0 = CFrame.new(-1, -1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.C1 = CFrame.new(-0.5, 1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.Parent = torso            
    end
    print("delay? :D")
    wait(5)
end)

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

I Believe the issue was the way you where glueing to the Torso.

Try this :D

function waitForChild(parent, childName)
    local function round(n, dp)
        local dp = dp or 2
        return math.floor((n * (10 ^ dp)) + .5) / (10 ^ dp)
    end
    local child = parent:FindFirstChild(childName)
    local expired = 0
    while not child do
        if expired < 3 then
            expired = expired + wait(0)
        elseif expired >= 3 and expired < 7 then
            local t = tick()
            parent.ChildAdded:wait()
            expired = expired + (tick() - t)
        else
            print("waitForChild >> Failed to find " .. parent.Name .. "." .. childName .. " after 7 seconds :(")
            return nil
        end
        child = parent:FindFirstChild(childName)
    end
    print("waitForChild >> Found " .. parent.Name .. "." .. childName .. (expired <= 0 and " immediately." or " after " .. round(expired, 3) .. "s."))
    return child
end

local char = script.Parent
local hum = waitForChild(char, "Humanoid")
local torso = waitForChild(char, "Torso")
local mods = {}

hum.Died:connect(function()
    print(script.Name .. " >> Attempting ragdoll for " .. char.Name .. "...")
    hum.Parent = nil
    local head = char:FindFirstChild("Head")
    local rarm = char:FindFirstChild("Right Arm")
    local larm = char:FindFirstChild("Left Arm")
    local rleg = char:FindFirstChild("Right Leg")
    local lleg = char:FindFirstChild("Left Leg")
    local Torso = char:FindFirstChild("Torso")
    if head then
        local neck = Instance.new("Weld")
        neck.Name = "Neck"
        neck.Part0 = Torso
        neck.Part1 = head
        neck.C0 = CFrame.new(0, 1.5, 0)
        neck.C1 = CFrame.new()
        neck.Parent = Torso
    end
    if rarm then
        rarm.CFrame = torso.CFrame * CFrame.new(1.5, 0, 0)
        local j = Instance.new("Glue")
        j.Name = "RightShoulder"
        j.Part0 = Torso
        j.Part1 = rarm
        j.C0 = CFrame.new(1, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.C1 = CFrame.new(-0.5, 0.5, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.Parent = Torso
    end
    if larm then
        larm.CFrame = torso.CFrame * CFrame.new(1.5, 0, 0)
        local j = Instance.new("Glue")
        j.Name = "LeftShoulder"
        j.Part0 = Torso
        j.Part1 = larm
        j.C0 = CFrame.new(-1, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.C1 = CFrame.new(0.5, 0.5, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.Parent = Torso        
    end
    if rleg then
        rleg.CFrame = torso.CFrame * CFrame.new(.5, -2, 0)
        local j = Instance.new("Glue")
        j.Name = "RightHip"
        j.Part0 = Torso
        j.Part1 = rleg
        j.C0 = CFrame.new(1, -1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.C1 = CFrame.new(0.5, 1, 0, 0, 0, 1, 0, 1, 0, -1, -0, -0)
        j.Parent = Torso            
    end
    if lleg then
        lleg.CFrame = torso.CFrame * CFrame.new(-.5, -2, 0)
        local j = Instance.new("Glue")
        j.Name = "LeftHip"
        j.Part0 = Torso
        j.Part1 = lleg
        j.C0 = CFrame.new(-1, -1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.C1 = CFrame.new(-0.5, 1, 0, -0, -0, -1, 0, 1, 0, 1, 0, 0)
        j.Parent = Torso            
    end
    print("delay? :D")
    wait(5)
end)



I also found that this way makes the anchoring of the torso useless

Ad

Answer this question