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

Attempt to index a nil value fix?

Asked by 9 years ago

I found a vampire script and I'm attempting to modify it, but when i go to try and make it remove the players shirt, It works, but the rest of the script doesnt execute due to the error. Workspace.Player.vmp.Vampire:13: attempt to index a nil value

if script.Parent.Name == "vmp" then
    wait(5)
    e = script.Parent.Parent:children()
    for i = 1,#e do
        if e[i]:IsA("Part") then
            wait(1)
            e[i].BrickColor = BrickColor.new("White")
            e[i].Parent:FindFirstChild("Head"):FindFirstChild("face").Texture="http://www.roblox.com/asset/?id=7323991"
            e[i].Parent:FindFirstChild("Torso").BrickColor = BrickColor.new("Black")
            e[i].Parent:FindFirstChild("Right Leg").BrickColor = BrickColor.new("Black")
            e[i].Parent:FindFirstChild("Left Leg").BrickColor = BrickColor.new("Black")
            e[i].Parent:FindFirstChild("Torso"):FindFirstChild("roblox").Texture=""
            e[i].Parent:FindFirstChild("Shirt"):remove()
        end
    end
    script.Parent = script.Parent.Parent
end

local larm = script.Parent:FindFirstChild("Left Arm")
local rarm = script.Parent:FindFirstChild("Right Arm")

target = nil

function findNearestTorso(pos)
    local list = game.Workspace:children()
    local torso = nil
    local dist = 1000
    local temp = nil
    local human = nil
    local temp2 = nil
    for x = 1, #list do
        temp2 = list[x]
        if (temp2.className == "Model") and (temp2 ~= script.Parent) and (temp2:findFirstChild("vmp") == nil) then
            followed = false
            z = Workspace:children()
            for x = 1,#z do
                if z[x]:findFirstChild("vmp") and z[x] ~= script.Parent then
                    if z[x].vmp.Value == temp2 then
                        followed = true
                    end
                end
            end
            if not followed then
                temp = temp2:findFirstChild("Torso")
                human = temp2:findFirstChild("Humanoid")
                if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
                    if (temp.Position - pos).magnitude < dist then
                        torso = temp
                        dist = (temp.Position - pos).magnitude
                    end
                end
            end
        end
    end
    return torso
end

function onTouched(part)
    if debounce then return end
    debounce = true
    print("Touched", part.Parent:findFirstChild("Torso"),target)
    if part.Parent:findFirstChild("Torso") then
        if part.Parent.Torso == target then
            script.Parent.Animate.Disabled = true
            part.Parent.Humanoid.PlatformStand = true
            g = Instance.new("BodyGyro")
            g.Parent = part.Parent.Torso
            g.cframe = CFrame.new(Vector3.new(0,0,0), Vector3.new(0,-5000000,0))
            g.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
            g.Name = "bg"
            w = Instance.new("Weld")
            w.Parent = script.Parent.Head
            w.Part0 = part.Parent.Head
            w.Part1 = w.Parent
            w.C0 = CFrame.new(0,.5,1) * CFrame.fromEulerAnglesXYZ(0,0,-2)
            script.Parent.Torso["Left Shoulder"].DesiredAngle = -3
            script.Parent.Torso["Right Shoulder"].DesiredAngle = 3
            script.Parent.Torso["Left Hip"].DesiredAngle = -.5
            script.Parent.Torso["Right Hip"].DesiredAngle = .5
            wait(4)
            if script.Parent.Humanoid.Health ~= 0 then
                b = Instance.new("ObjectValue")
                b.Parent = part.Parent
                b.Name = "vmp"
                script:clone().Parent = b
            end
            g:Remove()
            w:Remove()
            script.Parent.Animate.Disabled = false
            part.Parent.Humanoid.PlatformStand = false
            wait(.5)
        end
    end
    debounce = false
end


if script.Parent:findFirstChild("Head2") == nil then
    character = script.Parent
    a = character.Head:clone()
    character.Head.Transparency = 1
    a.Parent = character
    a.Name = "Head2"
    b = Instance.new("Weld")
    b.Parent = a
    b.Part0 = character.Head
    b.Part1 = a
end

script.Parent.Torso.Touched:connect(onTouched)

while true do
    wait(.1)
    target = findNearestTorso(script.Parent.Torso.Position)
    if target ~= nil and not attacking then
        script.Parent.vmp.Value = target.Parent
        mag = (script.Parent.Torso.Position - target.Position).magnitude
        script.Parent.Humanoid:MoveTo(target.Position+script.Parent.Torso.CFrame.lookVector*2, target)
        e = script.Parent:children()
        for i = 1,#e do
            if e[i]:IsA("BasePart") and e[i].Name ~= "Head" then
                e[i].Transparency = 0 + (mag/50)
            end
            if e[i]:IsA("Hat") then
                e[i].Handle.Transparency = 0 + (mag/50)
            end
        end
    else
        script.Parent.Humanoid:MoveTo(script.Parent.Torso.Position, script.Parent.Torso)
    end
end

If i messed up let me know im new to this forum. Please help me fix line 13

0
It did cut some of it off but i dont know how to fix that. My Apologies. orangebeak 5 — 9y

1 answer

Log in to vote
4
Answered by 9 years ago

Instead, add an IF statement to check if it exists.

if e[i].Parent:FindFirstChild("Shirt") then
e[i].Parent:FindFirstChild("Shirt"):Destroy() -- :remove() is deprecated.
-- Consider this: In local scripts, use remove(), but we are using normal script.
end

Hope this helps! Thanks, marcoantoniosantos3.

0
Ah, ya beat me to it. Exactly what I was gonna suggest, lol. Upvoted tho dyler3 1510 — 9y
0
Thank you :) orangebeak 5 — 9y
1
Line 2 need only be: e[i].Parent.Shirt:Destroy() (Remove is Deprecated). We already know it exists, no need to check for it again. adark 5487 — 9y
0
adark, just because we made sure pants exist doesnt mean there are shirt. Well take a look at the full code again. marcoantoniosantos3 200 — 9y
0
orange press the accept answer button if this fixed your problem. marcoantoniosantos3 200 — 9y
Ad

Answer this question