Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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 comment

1

It may be because Right Leg doesn't exist. Try WaitForChild instead.

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.