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

My variable turns out 'nil' when is not, how do i fix this?

Asked by
Xianon 105
8 years ago

signal is nil so i can't disconnect it from RBLXScriptSignal, it returns this: " ServerScriptService.ProceduralGen:50: attempt to index global 'signal' (a nil value)". I don't get it...

local closeByCheck = function(ply,n)
    print("Called")
 local signal = ply:FindFirstChild("Right Leg").Touched:connect(function(otherPart)
    signal:disconnect()
    print("Touched")
    print(otherPart.Parent)
    if otherPart.Parent == game.Workspace.Grid then
        print("Part is grid")
        if (ply.HumanoidRootPart.Position - otherPart.Position).magnitude < startRange then
            print("Part is close")
            print("N is: "..n)
            closeByPart[n] = otherPart
        end
    end

end)
return true
end
1
It may be because Right Leg doesn't exist. Try WaitForChild instead. TheDeadlyPanther 2460 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

It's because signal isn't local in the function scope

A suitable fix is to introduce signal as a local before creating the function

local closeByCheck = function(ply,n)
    print("Called")
    local signal
    signal = ply:FindFirstChild("Right Leg").Touched:connect(function(otherPart)
    signal:disconnect()
    print("Touched")
    print(otherPart.Parent)
    if otherPart.Parent == game.Workspace.Grid then
        print("Part is grid")
        if (ply.HumanoidRootPart.Position - otherPart.Position).magnitude < startRange then
            print("Part is close")
            print("N is: "..n)
            closeByPart[n] = otherPart
        end
    end

end)
return true
end
Ad

Answer this question