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

Why does this keep returning nil?

Asked by
Zerio920 285 Moderation Voter
10 years ago
local deb = false

script.Parent.Touched:connect(function(h)
    if h.Parent:FindFirstChild("Humanoid") then
        if deb == true then return end
        deb = true
script.Parent.Sound4:Play()
        local player = game.Players:GetPlayerFromCharacter(h.Parent)
        if player.Character:FindFirstChild("Left Leg") or player.Character:FindFirstChild("Right Leg") then
            if player.Character:FindFirstChild("Left Leg") then
            player.Character["Left Leg"]:Destroy()
                end
            if player.Character:FindFirstChild("Right Leg") then
            player.Character["Right Leg"]:Destroy()
            end
            end
            elseif player.Character:FindFirstChild("Left Leg") ~= nil and player.Character:FindFirstChild("Right Leg") ~= nil then
            if player.Character:FindFirstChild("Torso") then
            player.Character.Torso:Destroy()
            end
    end
    deb = false
    end)

Touching this brick is supposed to destroy the player's legs. If they touch it again while they don't have any legs, their torso will be destroyed. The part where the legs get destroyed works fine, however for some reason I get an "attempt to index field 'Parent' (a nil value)" when I touch it again.

0
Wait, I misplaced the position of an "end". This error still occurs after fixing that though. Zerio920 285 — 10y

2 answers

Log in to vote
0
Answered by 10 years ago
local ready = true --starting with a debounce var as true simplifies the debounce if statement. I called it 'ready' to increase human readability. ('if condition and ready' reads much better)

script.Parent.Touched:connect(function(h)
    if h.Parent:FindFirstChild("Humanoid") and ready then --verify humanoid and debounce
        ready = false --switch debounce
        script.Parent.Sound4:Play()
        --you didn't really need this line, at least for what you included in ScriptingHelpers
        if h:FindFirstChild("Left Leg") or h:FindFirstChild("Right Leg") then --h is still the character, so you don't have to go through the player.
            if h:FindFirstChild("Left Leg") then --this will always destroy the left leg first. You can switch this if you want the right leg destroyed first.
                h["Left Leg"]:Destroy()
            else
                h["Right Leg"]:Destroy()
            end
        elseif h:FindFirstChild("Torso") then
            h["Torso"]:Destroy() --you used (paraphrased) h.Torso:Destroy(). That's likely your problem
        end
        ready = true
    end
end)
0
h.Parent is the character, not h I think. After fixing that, still getting the same error. Also, it destroys both the legs and torso rather than just the legs. Zerio920 285 — 10y
0
Same error message, too? GoldenPhysics 474 — 10y
0
Yeah that's what I said. Zerio920 285 — 10y
0
Is there a line number with it? GoldenPhysics 474 — 10y
Ad
Log in to vote
-1
Answered by 10 years ago

Its not h use hit

view source

local deb = false

script.Parent.Touched:connect(function(hit)

if hit.Parent:FindFirstChild("Humanoid") then

    if deb == true then return end

    deb = true

script.Parent.Sound4:Play()

    local player = game.Players:GetPlayerFromCharacter(hit.Parent)<---only use this for GUIS Touch to make a gui pop up

Use this

player = hit.Parent if player:FindFirstChild("Left Leg") or player:FindFirstChild("Right Leg") then

        if player:FindFirstChild("Left Leg") then

        player["Left Leg"]:Destroy()

            end

        if player:FindFirstChild("Right Leg") then

        player[Right Leg"]:Destroy()

        end

        end

        elseif player:FindFirstChild("Left Leg") ~= nil and player:FindFirstChild("Right Leg") ~= nil then

        if player:FindFirstChild("Torso") then

        player.Torso:Destroy()

        end

end

deb = false

end)

--hit.Parent means Character that touch the part that is in the workspace

0
It's confusing cause the code's seperated, can you retype the thing with all the code in one place instead of parts of the code being outside and some being inside? Zerio920 285 — 10y
0
Sorry can't on ipad if i was on pc it will be easy try doing it type the script in Prince67891 0 — 10y

Answer this question